Java 在 MongoCollection<Document> 中查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30625380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find in MongoCollection<Document>
提问by jimakos17
I have a MongoCollection<Document>
in which I assign a collection.
I'm trying to find a user by his id.
我有一个我MongoCollection<Document>
在其中分配了一个集合。我试图通过他的 id 找到一个用户。
user = (Document) usersCollection.find(new Document("_id", username));
with that I'm getting an error
我收到了一个错误
java.lang.ClassCastException: com.mongodb.FindIterableImpl cannot be cast to org.bson.Document
java.lang.ClassCastException: com.mongodb.FindIterableImpl 不能转换为 org.bson.Document
When I try
当我尝试
BasicDBObject query = new BasicDBObject();
BasicDBObject fields = new BasicDBObject("_id", username);
usersCollection.find(query, fields);
I'm getting an error
我收到一个错误
The method find(Bson, Class) in the type MongoCollection is not applicable for the arguments (BasicDBObject, BasicDBObject)
MongoCollection 类型中的 find(Bson, Class) 方法不适用于参数 (BasicDBObject, BasicDBObject)
采纳答案by chridam
Try to create a filter to pass to the find()
method to get a subset of the documents in your collection. For example, to find the document for which the value of the _id
field is test
, you would do the following:
尝试创建一个过滤器以传递给该find()
方法以获取集合中文档的子集。例如,要查找_id
字段值为 的文档test
,您可以执行以下操作:
import static com.mongodb.client.model.Filters.*;
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("mycoll");
Document myDoc = collection.find(eq("_id", "test")).first();
System.out.println(myDoc.toJson());
回答by Amit Das
Do this -
做这个 -
MongoClient client = new MongoClient();
DBObject resultObject = new BasicDBObject("_id", username);
MongoDatabase database = client.getDatabase("DBNAME");
MongoCollection<Document> collection = database.getCollection("COLLECTION_NAME");
DBObject dbObject = new BasicDBObject("_id", username);
resultObject = collection.find(dbObject).next();
String result = resultObject.get(YOUR_COLOUM_NAME);
回答by aahoogendoorn
Your issue is that you assume that the find()
method returns a single Document
. It doesn't. It returns a list of them.
您的问题是您假设该find()
方法返回单个Document
. 它没有。它返回它们的列表。
In MongoDB 2 Java driver there was a method on the DBCollection
class named findOne()
. In the MongoDB 3 Java driver API, the findOne()
method isn't there. So your new code for finding exactly one document becomes similar too this one:
在 MongoDB 2 Java 驱动程序中,有一个DBCollection
名为findOne()
. 在 MongoDB 3 Java 驱动程序 API 中,该findOne()
方法不存在。因此,您用于查找一个文档的新代码也变得与此类似:
collection.find(eq("_id", 3)).first()
where eq("_id", 3)
is called a filter on your collection.
whereeq("_id", 3)
被称为您收藏的过滤器。
回答by Minal
MongoCollection<Document> filterCriteriaDoc = mongoDatabase.getCollection("documentName");
Document filterDoc = new Document();
filterDoc.put("col1", "value");
filterDoc.append("col2", "value");
filterDoc.append("col2", "value");
Iterator<Document> iter = filterCriteriaDoc.find(filterDoc).iterator();
iter.next()
can give you document .
iter.next()
可以给你文件。