[英]MongoDB Aggregation - query collections that satisfies all array of objects not at least one
[英]MongoDB Aggregation - Query array of objects
我有一個包含這些數據的集合:
[
{
_id: "123",
number: 10,
users: [
{
amount: 20
}
]
},
{
_id: "456",
number: 20,
users: [
{
amount: 10
}
]
}
]
我需要找到users[0].amount
大於或等於number
的文檔。 我有以下 Mongoose 查詢:
Model.aggregate([
{
$match: {
$expr: {
$and: [
{ $gte: ["$users.0.amount", "$number"] },
...
]
}
}
}
]);
但是,這似乎沒有正確過濾文檔。 我假設$users.0.amount
不是正確的語法。 我怎樣才能解決這個問題?
$expr需要聚合框架的語法,所以你必須使用$arrayElemAt代替:
{ $gte: [ { $arrayElemAt: [ "$users.amount", 0] }, "$number" ] }
其中users.amount
是一個數字數組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.