0

I have the following mongo object:

{
  "_id" : ObjectId("530a40b72098b01011000002"),
  "content" : "hellp world",
  "comments" : [
    {
      "_id" : ObjectId("530a419f5dfb071912000003"),
      "data" : {
        ...
      }
    },
    {
      "_id" : ObjectId("530a419f5dfb071912000004"),
      "data" : {
        ...
      }
    },
  ],
}

Is there any way I can query for it by using an _id that is inside the comments array?

user3060636
  • 262
  • 4
  • 15
  • 1
    `$where : { comments._id : "val"}`? – u_mulder Feb 23 '14 at 19:09
  • 1
    possible duplicate of [MongoDB extract only the selected item in array](http://stackoverflow.com/questions/3985214/mongodb-extract-only-the-selected-item-in-array) – JohnnyHK Feb 23 '14 at 21:07
  • @u_mulder (and the fool who up-voted) absolutely not and you need to **read** the documentation on [$where](http://docs.mongodb.org/manual/reference/operator/query/where/) – Neil Lunn Feb 23 '14 at 22:15
  • Agreeing with the duplicate report from @JohnnyHK, this is a common question with a common answer. – Neil Lunn Feb 23 '14 at 22:17

1 Answers1

0

With something like this, it should work:

.find({ 'comments._id': 530a419f5dfb071912000004 }).select('comments.data').run(function(err, result){ //blablabla })

Brice
  • 786
  • 4
  • 16
  • good, it seems to work. I see you are using mongoose, do you know if I can get just the exact comment object, instead of the whole thing? – user3060636 Feb 23 '14 at 19:17
  • I edited with what I'm thinking, but not sure at all as I'm learning mongoose/js/node/.. currently – Brice Feb 23 '14 at 19:34