Ravi Raj Ravi Raj - 3 years ago 211
PHP Question

How can i query mongodb with key as token in php?

Here is the mongo Document.
what code do i need to extract all data with associated token.

{
"_id": {
"$oid": "59cfb47b78a26d86cc5fdd7a"
},
"event": "Coding",
"data": [
{
"first_name": "Chanchal",
"last_name": "Roshan",
"email": "chan@gmail.com",
"phone": "75044",
"college": "AIT",
"members": "1",
"amount": "25",
"branch": "IT",
"token": "684695669",
"event": "Coding"
},
{
"first_name": "Ravi",
"last_name": "Raj",
"email": "i@gmail.com",
"phone": "89746",
"college": "AIT",
"members": "1",
"amount": "250",
"branch": "IT",
"token": "128027522",
"event": "Coding"
}
],
"auth_token": "z1iZ0LnDHFhs48PlYjNHyCjeh7y1"


}

I am new to php, sorry for such basic questions.

Answer Source

I am assuming you mean given token XXX, find all documents where an element of the dataarray contains token="XXX" and return just that element of the array, not the whole array. This should work. Note data.tokenand the use of the $positional operator in the projection (arg 2):

db.foo.find({"data.token":"XXX"},{"data.$":1});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download