I have the following collection:
{
_id: 1,
docs: [{
name: file1,
labels: [label1, label2, label3, label4]
},{
name: file2,
labels: [label3, label4]
},{
name: file3,
labels: [label1, label3, label4]
}]
}
When a user searches for labels, I need to get all the docs that have those labels.
So if a user searches for "label1" and "label3", I need to get "file1" and "file3". At the moment I have the following code:
var collection = db.collection(req.user.username);
collection.aggregate([
// Unwind each array
{ "$unwind": "$docs" },
{ "$unwind": "$docs.labels" },
// Filter just the matching elements
{
"$match": {
"docs.labels": { "$all": ["label1", "label3"] }
}
}
]).toArray(function (err, items) {
res.send(items);
});
This works fine if I only search for "label1" or "label3", but not the two together. Can you please help me with this, since I haven't found an answer that works myself.
mongoosemodule?