MongoDB在查找特定文档? [英] MongoDB finding a speciffic document?
问题描述
由于大多数教程已经过时,而且我的MongoDB版本(最新版本)没有BasicDBObject,因此我正在努力查找特定文档.
I'm struggling to find a specific document, as most tutorials are outdated, and my MongoDB version (the latest) does not have BasicDBObject.
我正在使用BSON,这是我的尝试,
I'm using BSON, here is my attempt,
public Document getPlayer(UUID uuid) {
Document toFind = new Document("id", uuid);
MongoCursor<Document> c = players.find(toFind).iterator();
while (c.hasNext()) {
if (toFind.equals(c)) {
return c;
}
}
return null;
}
我完全知道这是错误的,但是我只是不知道如何在MongoDB上找到任何信息.
I'm fully aware this is wrong, but I just do not know how to find any information on MongoDB.
推荐答案
For normal queries, use the Filters
utility class
players.find(Filters.eq("id", id))
注释后当我在这个答案上发现一个绿色的钩子时,我想您已经解决了,但是:请确保在您的项目中包括正确的驱动程序版本.具体来说,您需要3.x系列的驱动程序才能使用更现代的界面.
Edit after comment: as I find a green hook on this answer, I guess you already solved it, but nevertheless: make sure, that you include the correct driver version in your project. Specifically, you need a driver of the 3.x series to use the more modern interface.
当前的maven依赖项是:
The current maven dependency is:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.9.1</version>
</dependency>
完全合格的
Filters
实际上是com.mongodb.client.model.Filters
.
Filters
fully qualified is actually com.mongodb.client.model.Filters
.
这篇关于MongoDB在查找特定文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!