so i want to run a mongoose query to find posts where all the searcharray tags are present.
The tags vary in amount.
Currently this returns posts where any of the tags are present.
Post.find({
'tags.name': {
$in : searcharray
}
}, function(err, post) {
console.log(post);
}
);
I checked the docs and couldn't quite piece this one together.
Thanks
You want $all
, which is basically an $and
operation with shorter syntax much as $in
is an $or
operation with shorter syntax:
Post.find({"tags.name": { "$all": searcharray } }, function(err, posts) {
console.log(posts);
});
That requires that your "tags" array has members matching "name" for "all" of the items specified in the searchArray
list.
As an "or" condition the $in
whould just recall any documents that contained at least one of the items, so the "and" condition means all of the items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With