0

How can I do a select for all documents that have a parent.id that is equal to 3? I try doing this.parents.id == 3, but it doesn't seem to be working...

[parents] => Array (
    [0] => Array (
        [id] => 1
        [title] => Folder 1
    )
    [1] => Array (
        [id] => 3
        [title] => Folder 2
    )
)
4
  • and db.collectionName.find({'parents.id':3}) doesn't work either ? Commented Sep 26, 2011 at 9:43
  • and the php version : $collection->find(array('parents.id'=>3)) Commented Sep 26, 2011 at 9:45
  • PHP version worked - thank you! I'm a mongo newbie, sorry :P Commented Sep 26, 2011 at 21:29
  • No pb, everyone is a beginner in something. Even gurus ;) Commented Sep 26, 2011 at 22:46

1 Answer 1

1

OK, so the PHP input is actually unclear. The PHP you provided could be one of the following JSON objects:

Version 1:

{ parents: 
  [ 
    { id: 1, title: "Folder 1" }, 
    { id: 3, title: "Folder 2" } 
  ] }

Version 2:

{ parents: 
  { 0: { id: 1, title: "Folder 1" }, 
    1: { id: 3, title: "Folder 2" } 
} }

If you do find({'parents.id':3}) on version 1, this will work.

If you do find({'parents.id':3}) on version 2, this will not work.

The difference is not clear in PHP, so use the command-line and double-check.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! As Frederik posted above, the correct code in PHP is this: $collection->find(array('parents.id'=>3))
sorry it took so long, have been off the grid for a week :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.