gpt4 book ai didi

node.js - 如果第三个键为 true,则比较 mongodb 文档的两个键

转载 作者:太空宇宙 更新时间:2023-11-03 22:56:44 24 4
gpt4 key购买 nike

我有一个这样的文档,我正在使用 mongo 3.6 和聚合。

[
{
id: 123,
isLimit: "YES",
maxUserCanComment: 10,
commentCount: 6
},
{
id: 124,
isLimit: "NO",
maxUserCanComment: 0,
commentCount: 3
},
{
id: 125,
isLimit: "YES",
maxUserCanComment: 3,
commentCount: 5
}
]

我需要那个文档,如果 isLimit == "NO"就接受它,如果 isLimit == "YES",首先检查 commentCount < maxUserCanComment。我试过这个

[
$expr: { $gt: ["$commentCount", "$maxUserCanComment"] } // and also tried to add ithis in $cond
]

最佳答案

您需要使用$or此处的查询运算符则可以使用 $expr里面。

db.collection.find({
"$or": [
{ "isLimit": "NO" },
{
"isLimit": "YES",
"$expr": {
"$gt": [
"$commentCount",
"$maxUserCanComment"
]
}
}
]
})

MongoPlayground

关于node.js - 如果第三个键为 true,则比较 mongodb 文档的两个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60163224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com