Reputation: 245
I have an collection with an array of objects in it (I just put some fields out of it, the schema is working perfectly).
Collection Contacts:
title: {
type: String,
label: "Title",
max: 200
},
adresses: {
type: [Object],
optional: true
},
"adresses.$.id": {
type: String,
label: "ID"
},
"adresses.$.street": {
type: String,
label: "street",
decimal: true,
optional: true
}
When i do a :
db.contacts.findOne({_id: "59gXADmH9GLNDjELo"}, {adresses: {$elemMatch:
{id: "xpdYRKGGjHJLnCevM"}}});
on Mongo DB console, it returns:
{
"_id" : "59gXADmH9GLNDjELo",
"adresses" : [
{
"id" : "xpdYRKGGjHJLnCevM",
"street" : "FakeStreet123"
}
]
}
Like I want it - just 1 element of the array back.
When I do the same on Meteor (Browser Console):
Contacts.findOne({_id: "59gXADmH9GLNDjELo"}, {adresses: {$elemMatch:
{id: "xpdYRKGGjHJLnCevM"}}});
I get all elements of the array back. How to solve this? I want the same result as I got in Mongo DB.
Upvotes: 3
Views: 93
Reputation: 103365
You could use the ._find()
underscore method to retrieve just the document you want:
var doc = Contacts.findOne(
{_id: "59gXADmH9GLNDjELo"},
{adresses: {
$elemMatch: {id: "xpdYRKGGjHJLnCevM"}
}
}),
address = _.find(doc.adresses, function(address) {
return address._id === "xpdYRKGGjHJLnCevM"
});
Upvotes: 2