Reputation: 141
Data structure
{
users: [
{id: "aaa"},
{id: "bbb"},
{id: "ccc"}
]
},
{
users: [
{id: "111"},
{id: "222"},
{id: "333"}
]
},
array: ["111", "222", "333"]
I want to get the document where every "id" matches my array, like $in does. But I don't want matches where only two of three matches. So in this case, the query should return the second document.
Upvotes: 0
Views: 29
Reputation: 7741
One way:
const cursor = db.collection('inventory').find({
users: [{id: "111"},{id: "222"},{id: "333"}]
});
https://docs.mongodb.com/manual/tutorial/query-arrays/#query-an-array-for-an-element
Related Q: MongoDB Find Exact Array Match but order doesn't matter
Upvotes: 1
Reputation: 141
const userIds = ["abc", "123", ...]
I found this query to solve my problem.
{
$and: [
{users: {$elemMatch: {id: {$in: userIds}}}},
{users: {$size: userIds.length}}
]
}
Upvotes: 0