I am playing around with mongodb and having troubles with finding elements in an array
My collection looks like this
{
"_id" : ObjectId("4c4b1476238d3b4dd5000002"),
"username" : "pbakkum",
"first_name" : "Peter",
"last_name" : "Bakkum",
"age" : 21,
"addresses" : [
{
"name" : "home",
"street" : "588 5th Street",
"city" : "Brooklyn",
"state" : "CA",
"zip" : 11215
},
{
"name" : "work",
"street" : "588 5th Street",
"city" : "Brooklyn",
"state" : "CA",
"zip" : 11215
},
{
}
]
}
{
"_id" : ObjectId("4c4b1476238d3b4dd5000001"),
"username" : "kbanker",
"email" : "[email protected]",
"first_name" : "Kyle",
"last_name" : "Banker",
"hashed_password" : "bd1cfa194c3a603e7186780824b04419",
"addresses" : [
{
"name" : "home",
"street" : "588 5th Street",
"city" : "Brooklyn",
"state" : "NY",
"zip" : 11215
},
{
"name" : "work",
"street" : "1 E. 23rd Street",
"city" : "New York",
"state" : "NY",
"zip" : 10010
}
],
"payment_methods" : [
{
"name" : "VISA",
"last_four" : 2127,
"crypted_number" : "43f6ba1dfda6b8106dc7",
"expiration_date" : ISODate("2016-05-01T07:00:00Z")
}
],
"age" : 151
}
When I fire a query which is something like this.
db.users.find({"addresses.name":"home","addresses.state":"NY"});
the output of this query is a single record with the username as kbanker
This implies that it is exactly matching both the parameters "name":"home and addresses.state:NY and returning only one record.
However as per my understanding, this should return both the records since it should check for any of the parameters "name":"home" and addressess.state:NY in the enitre array and if any of them matches it should display that record.
This behavior of selecting just a single record defeats the purpose of $elemMatch attribute ?
Am I correct or something has changed in the new version of mongodb. I am using 3.2.5