gpt4 book ai didi

java - 从mongo集合中检索数据collection.find(query)

转载 作者:行者123 更新时间:2023-12-02 09:34:57 24 4
gpt4 key购买 nike

我需要从创建时间在 fromTime 和 toTime 范围内的集合中查找用户。用户集合为

db.users.find().limit(1).pretty() {
"_id": ObjectId("5c51a42efeb366379f27c679"),
"devices": [{
...
...
...
}
],
"creationDate": NumberLong("1548854318313"),
"isActive": true,
...
...
}

如果我在没有任何查询的情况下使用 findOne,它就可以工作

DBCollection collection = getDB().getCollection(“users”);
System.out.println(“find One= " + collection.findOne( ));

但是当我尝试添加查询时,它没有显示结果。

DBCollection collection = getDB().getCollection(“users”);
BasicDBObject query = new BasicDBObject(queryParams);
query.put(“creationDate”, BasicDBObjectBuilder.start("$gte", fromTime).add("$lte", toTime).get());
System.out.println(“collection.find(query));

哪里fromTime=1548854318310 和 toTime=1548854318315

它给出了

DBCursor{collection=DBCollection{database=DB{name='appdb'}, name='users'},
find=FindOptions{, batchSize=0, limit=0, modifiers=null, projection=null, maxTimeMS=0, skip=0,
sort=null, cursorType=NonTailable, noCursorTimeout=false, oplogReplay=false, partial=false}}

我做错了什么?

最佳答案

users.find({
created_at: {
$gte: ISODate("2010-04-29T00:00:00.000Z"), //any date / time
$lt: ISODate("2010-05-01T00:00:00.000Z") // any date / time
}
})
=> { "_id" : ObjectId("4c0791e2b9ec877893f3363b"), "name" : "example", "created_at" : "Sun May 30 2010 00:00:00 GMT+0300 (EEST)" }
//answer betweeen that date/time

关于java - 从mongo集合中检索数据collection.find(query),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57601179/

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