I am trying to retrieve values from a MongoDB collection using tornado and python.
The value which I am giving for search is in a list, I want the entire collection to be listed but the values other than the given values must not be shown in the list variable.
My data in table:
{
"_id" : ObjectId("564b313b3f32df05fc905570"),
"RequiredDate" : "17-11-2015",
"RequestedDate" : "17-11-2015",
"Pid" : [
"564b22373f32df05fc905564",
"5630baac3f32df134c18b682"
],
"email" : "abishek@gmail.com",
"UName" : "abishek",
"Uid" : "564b21003f32df05fc905563",
"phone" : "9988776655",
"PName" : [
"balu",
"prakash"
],
"Registration" : [
"TN 45 AG 5688",
"TN 45 AS 5655"
],
"Rid" : "564b313b3f32df05fc905570"
}
My query:
db.collection.find({"Rid" : "564b313b3f32df05fc905570"},{"Pid": {$elemMatch:{"Pid":"5630baac3f32df134c18b682","Registration": "TN 45 AS 5655"}}},{"_id": false}).pretty()
The desired output is:
{
"_id" : ObjectId("564b313b3f32df05fc905570"),
"RequiredDate" : "17-11-2015",
"RequestedDate" : "17-11-2015",
"Pid" : "5630baac3f32df134c18b682",
"email" : "abishek@gmail.com",
"UName" : "abishek",
"Uid" : "564b21003f32df05fc905563",
"phone" : "9988776655",
"PName" : [
"balu",
"prakash"
],
"Registration" : "TN 45 AS 5655",
"Rid" : "564b313b3f32df05fc905570"
}
Is $elemMatch
the correct way to get the output, or is there any other way to achieve the desired output.
Explanation:
In the suggested answer there a JSON object inside of the list and here I have a particular value where only the Pid is am aware of the Registration value is received from another table is matched with the value of Registration in this particular record and then displayed as a single record without showing other Pid and Registration.
And I am getting only the _id
in output, and not the entire modified record.