在将文档插入到数据库之前的MongoDB空集合

我试图实现一个函数来清空集合,每次我把文档填充到数据库中。 这对我不起作用。 我也尝试使用async.series ,但仍然没有运气。 为什么下面的代码不工作? 输出总是与db.users.find().count()给我0

 function emptyThenInsert(db) { UserSchema.collection.remove({}, function(err) { if(err) console.log(err); UserSchema.collection.insert(db, function(err, data) { console.log(data); if (err) console.log(err); UserSchema.collection.find({}, function(err, data) { console.log(data); if (err) console.log(err); }); }); }); } 

它工作,如果我拿出UserSchema.collection.remove

这似乎对我有用。

 function emptyThenInsert(db) { UserSchema.remove({}, function(err) { if(err) console.log(err); UserSchema.collection.insert(db, function(err, data) { if (err) console.log(err); UserSchema.collection.find({}, function(err, data) { if (err) console.log(err); }); process.exit(1); }); }); } 

尝试使用写关注:

 UserSchema.collection.insert(db, {w:1}, function(err, data) {