我有一个收藏夹,并且文档存储着date属性。我想解析日期并获取小于给定日期的值列表,通过降序对其进行排序并获取第一个。 我正在将Robo3T用于UI,从那里我可以查询
db.getCollection('myCollection').find({date:{"$lte":ISODate("2020-01-18T04:00:46Z")}}).sort({date:-1}).limit(1)
它产生正确的结果。 但是我无法使用Java代码来执行此操作,结果为空。这是我正在使用的Java代码。
MongoCollection<MyRegion> myCollection = database.getCollection('myCollection',MyRegion.class);
Bson queryOne = Filters.lte("date", DateTimeUtils.toISO8601UTC( date));
List<MyRegion> result = myCollection.find(queryOne).sort(Sorts.descending("date")).limit(1).into(new ArrayList<>());
Aslo也尝试过这个,
MyRegion result = myCollection.find(queryOne).sort(Sorts.descending("date")).first();
我是mongo查询的新手,被卡住了。请纠正我在哪里做错了。