Mongo DB:查询当前“有效”的文档。根据开始和结束日期 [英] Mongo DB: Query for documents currently "live" based on start and end date

查看:59
本文介绍了Mongo DB:查询当前“有效”的文档。根据开始和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的收藏集中的文档包括以下结构:

Documents in my collection include this structure:

"time" : { 
         "start" : ISODate("2013-06-10T20:31:48.694Z"), 
         "end" : ISODate("2013-12-11T20:31:48.694Z") 
}

根据查询的当前时间,我需要Mongo返回当前处于活动状态的文档(因为当前时间介于每个文档的开始时间和结束时间之间)。

Based off the current Time in which the query was made, I need Mongo to return which documents are currently "live" (in that the current time falls between the start and end time of each document).

我在该区域可以找到的唯一mongo查询是查询两个日期之间创建的文档:

The only mongo query I can find in this area queries for documents created between two Dates:

items.find({
    created_at: {
        $gte:"Mon May 30 18:47:00 +0000 2015",
        $lt: "Sun May 30 20:40:36 +0000 2010"
    }
})

推荐答案

就像这样:

var currentTime = new Date();
items.find({
    'time.start': {$lt: currentTime},
    'time.end': {$gt: currentTime}
});

将找到当前时间在开始之间的文档结束在文档中的时间。

Which will find the docs where the current time is between the start and end times in the doc.

这篇关于Mongo DB:查询当前“有效”的文档。根据开始和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆