MongoError:无法使用已结束的会话(仅限 1 个集合)

ken*_*der 2 session mongodb node.js express

我正在运行 mongodb 4.2、expressjs 4.15 和节点 12.13。

在我的 api 运行时,我对 mongo 配置进行了一些更改。从那时起,我在尝试查询特定集合时一直收到以下错误:

MongoError: Cannot use a session that has ended
at applySession (/Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/core/sessions.js:697:12)
at _command (/Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/core/wireprotocol/command.js:58:17)
at command (/Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/core/wireprotocol/command.js:28:5)
at Object.query (/Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/core/wireprotocol/query.js:57:3)
at Connection.query (/Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/cmap/connection.js:166:8)
at /Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/core/sdam/server.js:316:12
at Object.callback (/Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/cmap/connection_pool.js:347:7)
at /Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/cmap/connection_pool.js:497:23
at /Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/cmap/connection_pool.js:427:7
at callback (/Users/kendallyoder/Documents/api-three/node_modules/mongodb/lib/core/connection/connect.js:97:5)
Run Code Online (Sandbox Code Playgroud)

这只发生在一个集合上,所有其他集合都会提供数据就好了。

我假设有一个会话挂在某个地方我需要清除。我已经杀死并重新启动了我所有的开发进程,更新了 npm,重新安装了 mongodb,并清除了配置数据库中的所有“system.sessions”。

有任何想法吗?

蒙戈代码:

MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, client) {
// console.log("mongo is awake")
// console.dir(MongoClient,{depth:null})

if(err) callback(null, err)
if(client) {
  const db = client.db(dbName)

  if(action == 'find') {
    db.collection(collection).find(query).project(project).sort(sort).limit(limit).toArray().then((records, err) => {
      if(err) callback(null, err)
      if(records) callback(records, null)
    })
  }

  // other "actions"

  client.close()
} 
})
Run Code Online (Sandbox Code Playgroud)

小智 5

关闭回调内部的连接

if(action == 'find') {
    db.collection(collection).find(query).project(project).sort(sort).limit(limit).toArray().then((records, err) => {
        if(err) callback(null, err)
            if(records) callback(records, null)
                client.close() <=============== Close here

    })
}
Run Code Online (Sandbox Code Playgroud)