0

I got these documents:

  • id
  • name
  • friendList
    • id
    • name

I need to search in php (mongodb) for a friend, so name in friendList. How can I do this?

1
  • friendList is an array of {id, name} objects? Commented Jan 16, 2012 at 18:28

1 Answer 1

3

In the shell you'd do it like this:

> db.people.find({ "friendList.name" : /Joe/})

UPDATE: a proof:

> db.person.insert({name : 'scatman', friendList : [ {name: 'joe'}, {name: 'nick'}  ]});
> db.person.findOne()
{
    "_id" : ObjectId("4f155cafef7b8b0317a8ad17"),
    "name" : "scatman",
    "friendList" : [
        {
            "name" : "joe"
        },
        {
            "name" : "nick"
        }
    ]
}
> db.person.findOne({"friendList.name" : /jo/})
{
    "_id" : ObjectId("4f155cafef7b8b0317a8ad17"),
    "name" : "scatman",
    "friendList" : [
        {
            "name" : "joe"
        },
        {
            "name" : "nick"
        }
    ]
}
> 
Sign up to request clarification or add additional context in comments.

1 Comment

then either there's a bug in mongodb you're using or there's a bug in your code, or there's no one who's got Joe for his friend :) This query works just fine in mongodb 2.0.1.

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.