Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Searching for an array entry via its id in a MongoDB collection

Searching for an array entry via its id in a MongoDB collection

Problem

Is this possible:

i'd like to update one of the 'user stories' within a project, there can be many projects, and many user stories within a project. I'd like to select the project by its id, and then a user Story within that project based on its id and update its fields.

Question: What MongoDB query could I use?

MongoDB Database Cut

{
  "_id": ObjectId("4ecee95d6ce4004424000001"), //Project ID
  "owner": "test2",
  "stories": [
    {
      "_id": "75vUURHfE1bN9vkbwC2kR85s", //User story Id
      "title": "ok",
      "deadline": "10\/10\/2011",
      "description": "ok",
      "sp": "5",
      "value": "5",
      "roi": "1.00",
      "type": "story",
      "lane": 0
    },
    {
      "_id": "nTsa8x6vFJvdXfFtEHbcFIyl",
      "title": "test",
      "deadline": "10\/10\/2011",
      "description": "test",
      "sp": "5",
      "value": "5",
      "roi": "1.00",
      "type": "story",
      "lane": 0
    },
    {
      "_id": "i8Nd3HOhz7hZFzzInfCcapCe",
      "title": "test #2",
      "deadline": "10\/10\/2011",
      "description": "test #2",
      "sp": "5",
      "value": "5",
      "roi": "1.00",
      "type": "story",
      "lane": 0
    }
  ],
  "team": [

  ],
  "title": "ok"
}

I'm currently using socket.io, and on a socket event i retrieve the following data:

JSON

  var story = {
    _id: data.id,
    title: data.title,
    deadline: data.deadline,
    description: data.description,
    sp: data.sp,
    value: data.value,
    roi: data.roi,
    type: data.type
  }

I want to update the story within 'stories' in the mongo database using the above JSON object.

Problem courtesy of: Jack

Solution

In principle, you can use the $ operator (positional operator) to do that:

db.coll.update( {"stories._id":"i8Nd3HOhz7hZFzzInfCcapCe"}, 
                {$set:{"stories.$.roi":"1.25"}}, false, true )

However, I believe that _id is a reserved name; maybe you should not use _id for the embedded objects -- I had problems with this in the shell using MongoDB 2.0.1rc-1, so you might want to try sth. like this instead:

db.coll.update( {"stories.id":"i8Nd3HOhz7hZFzzInfCcapCe"}, 
                {$set:{"stories.$.roi":"1.25"}}, false, true )

There are some caveats: the $ operator works only on the first matched item, and it can't be used with upserts.

As a side note, from the information you give I have the feeling it's better to not embed the user stories and use a separate collection instead.

One reason is that concurrency will become painful if you use embedded collections (that is, if many people edit the same project at a time you need to somehow merge the arrays or use a locking mechanism).

Solution courtesy of: mnemosyn

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Searching for an array entry via its id in a MongoDB collection

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×