I'm trying to implement a function to empty the collection every time I populate document into the database. This doesn't work for me. I also tried using async.series
, but still no luck. Why doesn't this code below work? The output always with db.users.find().count()
gave me 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);
});
});
});
}
It works if I take out UserSchema.collection.remove
.
This seem to work for me.
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) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.