gpt4 book ai didi

javascript - 将 MongoDB $match 动态构建为字符串

转载 作者:行者123 更新时间:2023-11-30 20:14:04 24 4
gpt4 key购买 nike

我试图为我的 MongoDB 请求动态构建我的 $match,当我做一些简单的事情时,它工作得很好,就像这样:

var matchStr = {};
matchStr.status = { "$lte": 3 };
matchStr.checkout = { $gte: Math.round(new Date(d).getTime()/1000) }

然后我运行

bookingTable.aggregate([
{
$match: {
$and: [ matchStr ]
}
}, etc....

这给了一个很好的:

matchStr: {
"status": {
"$lte": 3
},
"checkout": {
"$gte": 1527669588
}
}

这一切都很好,但是如果我想把这样的东西放到 matchStr 中怎么办...

{ $or: [ { "managerDate": {$lte: managerLast} },  { "activityDate": {$lte: activityLast} } ] }
,
{ $or: [ { "expireDate": {$gt: oneDayBackward} }, { "status": {$lt: 9}} ] }
,
{ "status": { $in: [0, 1, 2, 9 ] } }

我该怎么做?

最佳答案

有多个syntax用于访问对象的属性

var matchStr = {}
matchStr.status = { "$lte": 3 }
matchStr.checkout = { "$gte": Math.round(new Date().getTime()/1000) }

matchStr["$or"] = [
{ "managerDate": { "$lte": "managerLast" }},
{ "activityDate": { "$lte": "activityLast" }}
]

或者如果你想推送到$or运算符

matchStr["$or"].push({ "managerDate": { "$lte": "managerLast" } })
matchStr["$or"].push({ "activityDate": { "$lte": "activityLast" } })

关于javascript - 将 MongoDB $match 动态构建为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52094873/

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