1

I have a collection like the following. I wan to get the users data only where fb_user_id is 2001.

How can I do this in PHP?

{
"_id": ObjectId("5024db89b7ac1c900e000000"),
"title": "This is a title",

"users": [
            {
              "name": "John",
              "email": "[email protected]",
              "fb_user_id": "1000"
            },
            {
              "name": "Jack",
              "email": "[email protected]",
              "fb_user_id": "2001"
            },

        ]

}
1
  • I tried like this $data = $collection->findOne(array("title" => "This is a title", "users.fb_user_id" => '2001'), array("users")); But it will return all users information, I want only user information whose fb_user_id is 2001. Please help me Commented Aug 13, 2012 at 5:38

2 Answers 2

2

Your could use the following mongodb query

{ "title.users.fb_user_id" : 2001 }

I case of more complex query you could use elemMatch query operator

{ 
   "title.users" : {$elemMatch : { "fb_user_id" : 2001, "name" : "John" } }
}

Something like that. Hope it helps

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

6 Comments

Here is reference mongodb.org/display/DOCS/… But it`s also possible to find it via "title.users.fb_user_id" : 2001 $elemMatch is for more that 1 expression will update the answer
Oh, I feel so silly. I didn't have my glasses on when I read your comment the first time, and completely misread your answer. Yes, you are correct. I didn't notice the outer top and bottom brackets, so I wasn't sure what you were trying to accomplish with only a "title.users": {...}. My apologies!
No problem, you actually help me to answer better for that questions. Thanks a lot.
I tried like this $data = $collection->findOne(array("title" => "This is a title", "users.fb_user_id" => '2001'), array("users")); But it will return all users information, I want only user information whose fb_user_id is 2001. Please help me
You can setup fields set to return, but in any case it will be in your document schema, for example you have an object {a:{b:{c:123}}} and you run query findOne({"a.b.c" : 123 },{"a.b.c" : 1"}) here {"a.b.c" : 1"} is the setup which fields to return. But you will get in in the schema of parent object. Like "title" : {"users" : [ ] } and so on
|
1

If you only want to return one match found within an embedded array (and not the rest of the array) you will need to either:

  • handle this in your application code (iterate the users array and find the matching element)

  • use the new positional operator support (SERVER-828) which will be available in the 2.2 version of MongoDB.

Example using the positional operator in the mongo 2.2 shell:

> db.mycoll.find({'users.fb_user_id':"2001"}, {_id:0, 'users.fb_user_id.$':1})
{
    "users" : [
        {
            "name" : "Jack",
            "email" : "[email protected]",
            "fb_user_id" : "2001"
        }
    ]
}

1 Comment

NB: MongoDB 2.2rc0 release candidate has been available for testing since mid-July.

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.