Mongodb, concat more updates in one query

There is a way to concat more updates? For example I would like to change more values in the same element. So having this...

{
    cc: [
          { user_id: "1", hasSeen:true}
         ,{ user_id: "2", hasSeen:false}
         ,{ user_id: "3", hasSeen:false}

    ]
    ,conversation: [{
        user_id: "1",
        text: "message by 1, to 2and3"
    }]
}

...I would like to push a new conversation object and also change all the hasSeen values.

For do the first point, no problem, I just push only a new conversation object. And it works...

    ...update(
        { _id : _param.conversation_id }
        ,{ $push:{ conversation:{user_id:"2",text:"message by 2, to 1,3"} }}
    )
    .exec(function(err, numAffected, rawResponse) {
    });

But I would like also to change the three "hasSeen" values in the same time. is it possible? Can I do it with one query? or i should split it in two queries?

ps: I use mongoose.


Currently the positional operator (which I think you will need here) does not work in such a manner that you can do a type of conditional update whereby you iterate through a list of $inc s or $set s to change those subdocument values.

There is a JIRA for such a thing that could possibly help you: https://jira.mongodb.org/browse/SERVER-6566?page=com.atlassian.jira.plugin.system.issuetabpanels:changehistory-tabpanel but it is filed under "features we are not sure of".

Best to split this up currently.

链接地址: http://www.djcxy.com/p/61948.html

上一篇: 如何使用单个查询来查询和更新文档?

下一篇: Mongodb,concat在一个查询中更新更新