Pradeep Banavara
Pradeep Banavara

Reputation: 1003

mongodb retrieving array values from a find query

Here's my mongodb document:

{ "FRIENDS" : [     
    { "LOC" : "13.039064033333332,77.5547927", "NAME" : "A1", "PHONE_NUMBER" : "817361155" } ],
 "MYID" : "349512956", 
"MYLOCATION" : "13.0389541,77.5549799", 
"OCCASION" : "cafe", 
"_id" : ObjectId("503d87150f43159357000001"), 
"average" : "13.0389541,77.5549799" }

Here's my query

> db.test_collection.find({"MYID":"349512956"},{"FRIENDS.PHONE_NUMBER":"817361155"});

and the output

{ "FRIENDS" : [ { "PHONE_NUMBER" : "817361155" } ], "_id" : ObjectId("503d87150f43159357000001") }

However, I want the entire friend document included in the result. LOC, NAME, PHONE_NUMBER. How do I get these ? Any help will be greatly appreciated. I'm searching through the monogo docs but am unable to find my answer.

Upvotes: 2

Views: 219

Answers (2)

slee
slee

Reputation: 534

To be more specific, the second argument selects the fields that you want outputted. See the actual implementation below:

> db.test_collection.find
function (query, fields, limit, skip, batchSize, options) {
    return new DBQuery(this._mongo, this._db, this, this._fullName, 
           this._massageObject(query), fields, limit, skip, batchSize, options || 
           this.getQueryOptions());
}

Upvotes: 0

Bob
Bob

Reputation: 1497

Try this

// i.e., select * from things where MYID=somethig and PHONE_NUMBER="foo"
db.test_collection.find({"MYID":"349512956", "FRIENDS.PHONE_NUMBER":"817361155"});

Upvotes: 3

Related Questions