I m 在Node提出申请,其中含有一些CRUD组成部分。 在我的一个数据物体上,我有一个<代码>save()方法,目的是更新记录,如果该物体在收集中发现过错,或者如果未更新新文件的话。 此外,如果做 up,我就想回头来回答Mongo制作的文件。
It seems that findAndModify would do this, and, indeed, it does return an _id value from the database. In my query clause, I am filtering by _id. If my data object doesn t have an id, Mongo correctly does an upsert, however, no matter what _id value it returns, in addition to the the keys I am setting in the update
clause, it also sets the _id on the document based on what value I used in the query
clause. Some code for clarity:
User.prototype.save = function(callback) {
var that = this;
var args = {
query : { _id: this.getId() }, //getId() returns empty string if not set
update : { $set : {
firstName : this.firstName,
lastName : this.lastName,
email : this.email
//_id : this.getId()
// which is blank, is magically getting added due to query clause
}},
new : true,
upsert : true,
fields : { _id : true}
};
this.db.collection(dbName).findAndModify(args, function(err, doc){
if(!that.getId()) {
that.setId(doc._id);
}
if (typeof(callback) === "function"){
callback.call(that);
}
});
}
我只想看一下最新消息的语气,这些话也是为了回到由Mongo生成的 _。 我不希望<条码>、条码>的数值。 附加条款如载于<代码>update地图。 是否有办法实现我正在得到的东西?