简体   繁体   English

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

[英]Building your MongoDB $match as a string dynamically

Im trying to build my $match dynamically for my MongoDB request, and when I do the simple stuff it works perfect, like this:我试图为我的 MongoDB 请求动态构建我的 $match,当我做简单的事情时,它工作得很好,就像这样:

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

And then I run the然后我运行

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

Which gives a nice:这给出了一个很好的:

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

So thats all great, but what if I want to put something like this into the matchStr...太好了,但是如果我想将这样的内容放入 matchStr 中怎么办...

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

How can I do that?我怎样才能做到这一点?

There are multiple syntax for accessing the property of an object访问对象的属性有多种 语法

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

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

or If you want to push to $or operator或者如果你想推送到$or运营商

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM