In my nodejs application, I want to find items matching labels.
I have an object with labels:
{
"name": "object1",
"labels": [
{"name": "label1"},
{"name": "label2"}
}
}
From this collection, I want to find all items matching the labels the user provides. (/api/findLabels/label1,label2)
At this point, I know I can find this in MongoDB with this query:
db.getCollection('items')
.find(
{'labels': {
$elemMatch:
{'name': 'label1', 'name': 'label2'}
}}
However, I seem to be having issues with building the query in Node.js.
At this point, I have:
Item.find(
{
'labels':
{
$elemMatch: labels
}
}).exec(function(err, items){
...
})
But how can I build the correct structure for the labels parameter? Setting a key more than once with a new value is just replacing the value, not adding it to the list, which makes sense.
Any ideas?