在 mongodb nodejs 应用程序上使用客户端会话时出现“不允许使用过期会话”错误

Zha*_* Yi 6 mongodb node.js

我的 mongodb 是 3.6,我的 nodejs 应用程序使用的是 mongo 客户端 3.0。下面是使用 mongo 客户端会话来确保因果关系的源代码:

const session = client.startSession();
  const col = client.db('test').collection('test');
  col
    .insert({a: true}, {w: 0, j: false, session})
    .then(() => {
      return col.count({session});
    })
    .then(ret => {
      console.log(ret);
    }).catch(err => console.error(err));
  session.endSession();
}
Run Code Online (Sandbox Code Playgroud)

运行此应用程序时出现以下错误。它无需为insertorcount命令指定会话即可工作。我想知道在这种情况下如何使用 session。

    { MongoError: Use of expired sessions is not permitted
    at executeOperation (/Users/joey/dev/mongodb/demo/node_modules/mongodb/lib/utils.js:390:13)
    at Collection.count (/Users/joey/dev/mongodb/demo/node_modules/mongodb/lib/collection.js:1889:10)
    at col.insert.then (/Users/joey/dev/mongodb/demo/index.js:33:18)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  name: 'MongoError',
  message: 'Use of expired sessions is not permitted' }
Run Code Online (Sandbox Code Playgroud)

Zha*_* Yi 7

我想我发现了我的代码有什么问题。我需要转到session.endSession();then 或 catch ,因为它是一个 promise 调用。