Mongoose 索引不会在 Mocha 测试中有一半时间被创建
Mongoose indexes don't get created half the time on Mocha tests
每当我 运行 我的 Mocha 测试时,它会在创建索引和不创建索引之间交替。我认为它以某种方式没有创建索引,因为测试可能在它完成之前有 运行,但由于它以这样的模式交替出现,我认为情况并非如此。我还认为这可能与我在每次测试开始时删除数据库有关,但这不应该只影响其他所有测试。
相关索引:
submissionSchema.index({ studentID: 1, assignmentID: 1 }, { unique: true });
删除数据库的代码:
before(function(done){
mongoose.createConnection(require(__dirname + '/../app/config').mongoURL, function(err){
if (err) throw err;
mongoose.connection.db.dropDatabase(function(err){
if (err) throw err;
done();
});
});
});
知道是什么原因造成的吗?
Blake Sevens 是对的。为了解决这个问题,我在删除数据库后重建了索引。
before(function(done){
mongoose.createConnection(require(__dirname + '/../app/config').mongoURL, function(err){
if (err) throw err;
mongoose.connection.db.dropDatabase(function(err){
if (err) throw err;
var rebuildIndexes = []
var models = Object.keys(mongoose.connections[0].base.modelSchemas);
models.forEach(function(model){
rebuildIndexes.push(function(cb){
mongoose.model(model, mongoose.connections[0].base.modelSchemas[model]).ensureIndexes(function(err){
return cb(err);
})
});
});
async.parallel(rebuildIndexes, function(err) {
if (err) throw err;
console.log('Dumped database and restored indexes');
done();
});
});
});
});
每当我 运行 我的 Mocha 测试时,它会在创建索引和不创建索引之间交替。我认为它以某种方式没有创建索引,因为测试可能在它完成之前有 运行,但由于它以这样的模式交替出现,我认为情况并非如此。我还认为这可能与我在每次测试开始时删除数据库有关,但这不应该只影响其他所有测试。
相关索引:
submissionSchema.index({ studentID: 1, assignmentID: 1 }, { unique: true });
删除数据库的代码:
before(function(done){
mongoose.createConnection(require(__dirname + '/../app/config').mongoURL, function(err){
if (err) throw err;
mongoose.connection.db.dropDatabase(function(err){
if (err) throw err;
done();
});
});
});
知道是什么原因造成的吗?
Blake Sevens 是对的。为了解决这个问题,我在删除数据库后重建了索引。
before(function(done){
mongoose.createConnection(require(__dirname + '/../app/config').mongoURL, function(err){
if (err) throw err;
mongoose.connection.db.dropDatabase(function(err){
if (err) throw err;
var rebuildIndexes = []
var models = Object.keys(mongoose.connections[0].base.modelSchemas);
models.forEach(function(model){
rebuildIndexes.push(function(cb){
mongoose.model(model, mongoose.connections[0].base.modelSchemas[model]).ensureIndexes(function(err){
return cb(err);
})
});
});
async.parallel(rebuildIndexes, function(err) {
if (err) throw err;
console.log('Dumped database and restored indexes');
done();
});
});
});
});