English 中文(简体)
MongoDB 认为AndModification() 增加了对更新条款的询问
原标题:MongoDB findAndModify() adds query to update clause

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地图。 是否有办法实现我正在得到的东西?

最佳回答

You can generate the _id client side, with new new require( mongodb ).ObjectID() Then you can just do a regular upsert (no need to do findandmodify) because you already have the _id.

However, if you are using findAndModify, keep in mind that the node driver accepts the arguments to this function positionally, not as an object (like in the regular mongo shell). The correct format to do findandmodify with the node driver looks like this:

Collection.findAndModification(criteria, 度,更新[,备选办法,反馈])

(options and callback are optional params). Full docs here: https://github.com/mongodb/node-mongodb-native/blob/master/docs/insert.md

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签