I am using MongoDB 3.0.5. I have a collection like
db.getCollection('mytest').insert([
{_id: 1, "records": [{"Name": "Joe", "Salary": 70000, "Department": "IT"}]},
{_id: 2, "records": [{"Name": "Henry", "Salary": 80000, "Department": "Sales"}, {"Name": "Jake", "Salary": 40000, "Department": "Sales"}]},
{_id: 3, "records": [{"Name": "Sam", "Salary": 90000, "Department": "IT"}, {"Name": "Tom", "Salary": 50000, "Department": "Sales"}]},
{_id: 4, "records":[{"Name": "Janice", "Salary": 80000, "Department": "Finance"}, {"Name": "Kale", "Salary": 95000, "Department": "IT"}]}
])
Now I could query a single field, say like people in the IT department -
> db.getCollection('mytest').find({"records.Department": {"$in": ["IT"]}}, {"records.$": 1})
{ "_id" : 1, "records" : [ { "Name" : "Joe", "Salary" : 70000, "Department" : "IT" } ] }
{ "_id" : 3, "records" : [ { "Name" : "Sam", "Salary" : 90000, "Department" : "IT" } ] }
{ "_id" : 4, "records" : [ { "Name" : "Kale", "Salary" : 95000, "Department" : "IT" } ] }
I really want to find the salaries in the departments of Finance and IT. But the query with more than one fields returns part of the desired results -
> db.getCollection('mytest').find({"records.Department": {"$in": ["IT", "Finance"]}}, {"records.$": 1})
{ "_id" : 1, "records" : [ { "Name" : "Joe", "Salary" : 70000, "Department" : "IT" } ] }
{ "_id" : 3, "records" : [ { "Name" : "Sam", "Salary" : 90000, "Department" : "IT" } ] }
{ "_id" : 4, "records" : [ { "Name" : "Janice", "Salary" : 80000, "Department" : "Finance" } ] }
I wonder if somebody could help with it. Thanks.