【问题标题】:mongodb java query where clause for datemongodb java查询日期的where子句
【发布时间】:2016-03-01 17:41:34
【问题描述】:

我是 MongoDB 新手,不知道如何实现以下目标。 收藏中的一份文件,

{ 
    "_id" : ObjectId("56cf4cf2997964f0d91aa373"), 
    "id" : "A", 
    "status" : "open", 
    "createdTime" : NumberLong(1456426212898)
}

我想使用以下 where 查询获取数据,

哪里 - status 开放 && (createdTime + 10 分钟)

以下代码适用于status。但不确定如何包含如上所示的时间。

for open `status` - {"status":"open"}

Java 代码,

    final Bson statusFilter = eq("status", "open");

    final Document projection = new Document();

    final Document sort = new Document();
    sort.append(CREATED_TIME, -1);

    final FindIterable<Document> operation = collection.find(filter).projection(projection).sort(sort);

但不确定,如何实现(createdTime + 10 分钟)

谢谢

【问题讨论】:

    标签: java mongodb mongodb-query


    【解决方案1】:

    应该是

    { $and:[{ "status":"open"},{"createdTime":{$lt:new Date().getTime() - (1000 * 60 * 10)}} ] }
    

    查询类似于where status=open &amp;&amp; createdTime&lt;(currentTime-10mins)

    注意:createdTime+10mins &lt; currentTime可以写成createdTime &lt; currentTime-10 mins

    【讨论】:

    • 这个答案被接受。但想知道如何使用查询中的字段,如 createdTime+10mins。
    猜你喜欢
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2021-01-10
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多