我想使用findAndModify
( upsert )来修改文档。我想这样做:
created_at
; created_at
。我使用findAndModify
如下所示,这将更新每created_at
upsert
这不是我想要的。
var acctsCollection = "accounts";
account.created_at = new Date();
account.newData = newData;
var newUpdate = {
$set : account
};
var options = {
upsert :true,
new: true,
};
collectionDriver.findAndModify(acctsCollection, query, newUpdate, options);
答案 0 :(得分:1)
您可以使用$setOnInsert:
var newUpdate = {
$set : {newData: newData},
$setOnInsert: {created_at: new Date()}
};