0
我有以下模式:貓鼬默認W/findOneAndUpdate
var UserSchema = new Schema({
username: { type: String, required: true, unique: true },
displayName: { type: String, required: true },
registered: { type: Boolean, required: true, default: false }
});
然後我嘗試插入並使用對象findOneAndUpdate:
var user = {
displayName: "Test User"
}
User.findOneAndUpdate({username: "test"}, user, {new: true, upsert: true}, function(err, newUser) {
callback(err, newUser);
});
用戶獲得正確創建(在第一次嘗試,因爲它不存在)。但是,註冊字段不會設置爲false。當我做findOneAndUpdate時應該發生這種情況嗎?從蒙戈查詢
結果:
> db.users.find().pretty()
{
"_id" : ObjectId("56255f415641ace1204de658"),
"username" : "Test",
"displayName" : "Test User"
}