我的 mongodb 是 3.6,我的 nodejs 应用程序使用的是 mongo 客户端 3.0。下面是使用mongo client session保证因果关系的源码:
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();
}
运行此应用程序时出现以下错误。它无需为 insert
或 count
命令指定 session 即可工作。我想知道在这种情况下如何使用 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' }
最佳答案
我想我发现了我的代码有什么问题。我需要将 session.endSession();
移动到 then 或 catch,因为它是一个 promise 调用。
关于node.js - 在 mongodb nodejs 应用程序上使用客户端 session 时出现 `Use of expired sessions is not permitted` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49807724/