gpt4 book ai didi

java - MongoDB 在 Java 中按日期查询

转载 作者:可可西里 更新时间:2023-11-01 09:06:37 26 4
gpt4 key购买 nike

<分区>

我正在尝试在 mongodb 中查询“日期”介于两个日期之间的文档。样本数据是:

{
"_id" : ObjectId("4fad0af6709fbc1d481c3e05"),
"ID" : NumberLong("200930746205085696"),
"text" : "Forgot my charger...:(",
"date" : ISODate("2012-06-14T10:49:57Z"),
"sentiment" : "NEG"
}

我的 Java 代码是:

DBCursor cursor = null;
DB db = connect();

Date startDate = new Date(System.currentTimeMillis() - (long)(numOfTimePeriods+1)*time);
Date endDate = new Date(System.currentTimeMillis() - (long)numOfTimePeriods*time);
DBObject query = new BasicDBObject();
query.put("date", new BasicDBObject("$gt", startDate).append("$lte", endDate));

cursor = db.getCollection("status").find(query);

但是游标对象没有结果。

查询对象如下所示:

{ "date" : { "$gt" : { "$date" : "2012-05-15T00:16:15.184Z"} , "$lte" : { "$date" : "2012-06-14T10:16:15.184Z"}}}

我怀疑问题是数据库中的日期表示。对此有何建议?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com