简体   繁体   English

Mongodb在日期上的总匹配

[英]Mongodb aggregate match on dates

I'm running an aggregate query, and after a few projections, I end up with documents that look like 我正在运行汇总查询,经过一些预测,最终得到的文档看起来像

{ _id: 561bed08f8f068cd237a3dc1,
updateDate: Mon Oct 12 2015 11:25:28 GMT-0600 (MDT),
manager: 561bdd37f8f068cd237a3da2,
viewDate: Mon Oct 26 2015 09:02:57 GMT-0600 (MDT) }

Please note, those dates are the console formatted output, when inspected with mongohub, the dates look like new Date(<timestamp>) . 请注意,这些日期是控制台格式的输出,当使用mongohub检查时,这些日期看起来像new Date(<timestamp>)

I want to match documents where the updateDate is greater than the viewDate , so I have this match: 我想匹配updateDate大于viewDate ,所以我有这个匹配项:

$match: {
    updateDate: { "$gt": new Date("$viewDate") }
}

However, it returns documents such as the one above, the updateDate is on the 12th, and the viewDate is on the 26th. 但是,它返回诸如上述文件之类的文档, updateDate在12号,而viewDate在26号。

I have tried a few variations, with and without the new Date() for example. 我尝试了一些变体,例如有无new Date()

You can't use the $match operator here. 您不能在此处使用$match运算符。 You need to use the $redact operator. 您需要使用$redact运算符。

db.collection.aggregate([
    { "$redact": { 
        "$cond": [ 
           { "$gt": ["$updateDate", "$viewDate"] }, 
           "$$KEEP", 
           "$$PRUNE"
        ]
    }}
])

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

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