mongodb能否找到';s lt运算符将文档字段作为参数
我有一份类似这样的文件mongodb能否找到';s lt运算符将文档字段作为参数,mongodb,Mongodb,我有一份类似这样的文件 { "_id":ObjectId("5bcef414b4305f4054571305"), "timestamp": ISODate("2018-10-23T10:12:36.755 Z"), "config" : { "expiry_duration" : NumberLong(10000) } } 我需要查找过期的文档,即其$datediff(time.Now(),$timestamp)config.expiratio
{
"_id":ObjectId("5bcef414b4305f4054571305"),
"timestamp": ISODate("2018-10-23T10:12:36.755 Z"),
"config" : {
"expiry_duration" : NumberLong(10000)
}
}
我需要查找过期的文档,即其$datediff(time.Now(),$timestamp)
config.expiration\u duration
我不清楚我是否需要使用聚合,或者我是否可以使用find self来实现这一点,您可以使用
.find()
方法来实现,但您需要运算符(在MongoDB 3.6中提供):
要获取当前日期,您可以在Mongo shell中键入new date()
如果您需要MongoDB<3.6的解决方案,可以使用.aggregate()
和管道阶段:
db.col.aggregate({
$redact: {
$cond: {
if: { $gt: [ { $subtract: [ ISODate("2018-10-23T16:39:06.266Z"), "$timestamp" ] }, "$config.expiry_duration" ] },
then: "$$KEEP",
else: "$$DESCEND"
}
}
})
谢谢@mickl,expr是丢失的部分。当这是一个纯粹的查找函数时,我很惊讶为什么我所有的搜索结果都会导致聚合。
db.col.aggregate({
$redact: {
$cond: {
if: { $gt: [ { $subtract: [ ISODate("2018-10-23T16:39:06.266Z"), "$timestamp" ] }, "$config.expiry_duration" ] },
then: "$$KEEP",
else: "$$DESCEND"
}
}
})