Reputation: 696
I am using match within aggregate to return any documents who have a matching elements with an embedded array and another array. An example:
Documents
{
name: "Frank",
favoriteColors: ["red", "pink", "orange"]
}
{
name: "Bob",
favoriteColors: ["blue", "red", "green"]
}
Array
let colors = ["blue", "maroon"];
So, basically, I want to find every person (document) that has either blue or maroon in their favorite colors.
Upvotes: 0
Views: 43
Reputation: 646
something like this:
db.myCollection.find({ favoriteColors: { $in: ["blue", "maroon"] } })
you can find more documentation here https://docs.mongodb.com/manual/reference/operator/query/in/#use-the-in-operator-to-match-values-in-an-array
Upvotes: 1
Reputation: 5853
Use the $in
operator to filter a range of values.
Insert script -
db.collection.insertMany([
{
"name": "Frank",
"favoriteColors": ["red", "pink", "orange"]
},
{
"name": "Bob",
"favoriteColors": ["blue", "red", "green"]
}]);
The query -
var query = {
favoriteColors: {
$in: ["blue", "maroon"]
}
};
db.collection.find(query);
Outputs -
/* 1 */
{
"_id" : ObjectId("5f12a14db00513b7c6ab6203"),
"name" : "Bob",
"favoriteColors" : [
"blue",
"red",
"green"
]
}
Upvotes: 2