我们有一个nodejs + mongodb应用程序,它已经在生产中运行了好几年,并且在多台机器上进行开发.在一个开发人员的机器上,我看到了错误
MongoError: collection already exists
Run Code Online (Sandbox Code Playgroud)
研究此错误表示仅在集合处于严格模式时尝试创建现有集合时才会发生此错误.我们不会在应用程序的任何地方调用mongo的严格模式,我们只能在一台机器上重现此错误.
导致此错误的代码如下:
var mongo = require('mongodb');
mongo.MongoClient.connect(config.mongoConnectionString, {w:1}, function(err, db) {
db.createCollection('accounts', function(err, collection) {
// "err" here is the error message.
});
});
Run Code Online (Sandbox Code Playgroud)
有没有办法覆盖mongo的默认strict: false值?是否有全局配置选项导致严格模式打开?我宁愿不修改代码来strict: false为每个集合指定只是为了启用单个开发人员.开发人员正在运行mongo v3.2
小智 2
您可以使用此禁用“严格”模式
var mongo = require('mongodb');
mongo.MongoClient.connect(config.mongoConnectionString, {w:1}, function(err, db) {
db.createCollection('accounts', {strict:false}, function(err, collection) {
// "err" here is the error message.
});
});
Run Code Online (Sandbox Code Playgroud)
你可以从这里阅读更多相关信息