If this is the structure of my documents...
{
"_id" : "8vJY4baMbdYkgHian",
"title" : "Cars",
"tradename" : [
{
"element" : "Audi"
},
{
"element" : "Mercedes"
}
]
}
... and I want to get all tradenames in a result array, this is what I am doing:
Collection.find(
{ tradename: { $elemMatch: { element: { $exists: true } } } }
).forEach(function(res) {
res.tradename.forEach(function() {
result.push({
id: res._id,
value: res.tradename[0].element,
type: 'article'
});
});
});
But it doesn't work.
undefined. If this is the case, I should implement some lines to ignore undefined values...