1

I have documents stored in collection that has form:

{
    _id: ObjectId(""),
    ...
    match:{
        status: 'active',
        list: [{
            _id: ObjectId("") // a referance to another document
            status: 'active' // can be active, seen, send
        },{
            ..
        }, ..]
    }
}

I do a collection('A').find() to retrieve this document and then retrieve all reference objects collection('B').find(). Everything is sent to browser for human inspection.

Lets say that there are 10 B's associated with particular document form collection A. In browser admin marks three of them to send out. Now the document A look like this:

{
    _id: ObjectId(""),
    ...
    match:{
        status: 'active',
        list: [{
            _id: ObjectId("") 
            status: 'seen' 
        },{
            _id: ObjectId("") 
            status: 'send'
        }, ..]
    }
}

Basically from our 10 B's three were marked as "send" but all the rest (7 in this example) marked as seen.

Now the A is sent back to server and should be updated. The simple way to do that would be

db.collection('A').update({_id: a._id}, {$set{match.list: a.match.list}}, function(err, result){..});

But there is a problem. The time for admin to inspect the set could be quite long, up to an hour. There is a high possiblity that a new associated B could be pushed in A.

I could pull out the items that first and then push the new values but that seems ugly. Is there a built-in way to handle this situation? I need to update only specific items in array.

I checked the $ positional operator but it seem that i will match only one object in array.

Kārlis Janisels
  • 1,265
  • 3
  • 18
  • 41
  • It's not really that clear what you are asking here. [Can you update multiple array items at once?](https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb) No you cannot really, and would generally use "Bulk Operations" to update each "matched" item, typically by a unique "id" rather than by position and rather than overwriting the array. In particular your "timings" statement is not very clear. I would suggest that you implement a "checkout" status or something like that to mark items waiting for particular feedback from an assigned person/operation. – Neil Lunn Jun 18 '17 at 13:11
  • Perhaps you should attempt to clarify a little further. But in general it seems like you are asking the "update multiple array elements" question, to which the basic approaches are on the existing question with it's answers already. – Neil Lunn Jun 18 '17 at 13:13
  • Thank you, this was the asnwer I was looking for. Now I am sure that there is no built in way to do this. Also answer bt @Daniel Cerecedo from question you provided is exactly what I need – Kārlis Janisels Jun 18 '17 at 15:18

0 Answers0