Reputation: 1648
I have a collection with this data:
[
{
_id: "123",
number: 10,
users: [
{
amount: 20
}
]
},
{
_id: "456",
number: 20,
users: [
{
amount: 10
}
]
}
]
I need to find documents where users[0].amount
is greater than or equal to number
. I have the following Mongoose query:
Model.aggregate([
{
$match: {
$expr: {
$and: [
{ $gte: ["$users.0.amount", "$number"] },
...
]
}
}
}
]);
However, this doesn't seem to be filtering the documents properly. I'm assuming $users.0.amount
isn't the correct syntax. How can I fix this?
Upvotes: 1
Views: 35
Reputation: 49945
$expr requires Aggregation Framework's syntax so you have to use $arrayElemAt instead:
{ $gte: [ { $arrayElemAt: [ "$users.amount", 0] }, "$number" ] }
where users.amount
is an array of numbers
Upvotes: 1