Reputation: 1648
I'm trying to find()
with Mongoose with the following query:
let users = await models.users.find({
inv: { $elemMatch: { name: "Some Item", name: "Another Item" } }
});
These documents should be found:
{
inv: [{ name: "Some Item", amount: 5 }]
}
//and
{
inv: [{ name: "Another Item", amount: 15 }]
}
//and
{
inv: [{ name: "Some Item", amount: 5 }, { name: "Another Item", amount: 15 }]
}
//and
{
inv: [{ name: "Some Item", amount: 5 }, { name: "Another Item", amount: 15 }, { name: "Different Item", amount: 1 }]
}
But these shouldn't:
{
inv: [{ name: "Different Item", amount: 1 }]
}
//and
{
inv: []
}
This works fine with regular MongoDB queries, but with Mongoose, this is a problem since you can't have multiple of the same properties in a JavaScript object (name
and name
in this case). How should I go about handling this?
Upvotes: 1
Views: 256