Please tell me what am I missing here:
this is the Doc I created:
fatDoc = {
name: "Babak",
personID : 555,
email : "babak@babak.name",
music : ["pink floyd", "muse", "garfunkel"],
food : ["free food", "yummy food", "mom food"],
addresses:
[ { type: "home",
street: "123 Main",
state: "NY",
city: "brooklyn"
},
{ type: "vacation",
street: "456 sunshine",
state: "CA",
city: "SanFran"
}
]
}
and this is the query I run on it:
db.coolkids.find({"addresses.type" : "home"}, {addresses:1}).pretty()
Question1: Why BOTH of addresses are getting returned as the result of this query?
Question2: How can I change it to ONLY return the first doc which is the “home” address and not the second member of the array?
your query matches this document, and you are getting back the field “addresses” which happens to be an array. In 2.0 the only way to get back less than the full array is via $slice operator but you would need to know the position of the element you want.
The way to get back just one element that matches your query would be using the new Aggregation Framework (available in development version 2.1, will be released with 2.2).
Aggregation Framework will allow you to “$unwind” arrays so that you can get back just the document with array element that matched your filter.