如何使用Java使用ObjectIds创建查询? [英] How to create query with ObjectIds using java?
问题描述
我有一组ID,例如:
["51eae104c2e6b6c222ec3432", "51eae104c2e6b6c222ec3432", "51eae104c2e6b6c222ec3432"]
我需要使用这组ID查找所有文档.
I need to find all documents using this set of ids.
BasicDBObject query = new BasicDBObject();
BasicDBList list = new BasicDBList();
ObjectId ob1 = new ObjectId("51eae100c2e6b6c222ec3431");
ObjectId ob2 = new ObjectId("51eae100c2e6b6c222ec3432");
list.add(ob1);
list.add(ob2);
query.append("_id", new BasicDBObject("$in", list));
此查询找不到任何内容,因为它与
This query can't find anything because it is same as
{ "_id" : { "$in" : [ { "$oid" : "51eae100c2e6b6c222ec3431"} , { "$oid" : "51eae100c2e6b6c222ec3432"}]}}
要找到必须的东西
{_id:{$in:[ObjectId("51eae100c2e6b6c222ec3431") , ObjectId("51eae104c2e6b6c222ec3432")]}}
但是我不知道如何使用Java在列表中制作ObjectId("51eae100c2e6b6c222ec3431")
but I don't know how to make ObjectId("51eae100c2e6b6c222ec3431")
in list using java
推荐答案
{ "$oid" : "51eae100c2e6b6c222ec3431"}
与ObjectId("51eae100c2e6b6c222ec3431")
相同,只是格式不同.
{ "$oid" : "51eae100c2e6b6c222ec3431"}
is the same as ObjectId("51eae100c2e6b6c222ec3431")
just in a different format.
有关不同格式,请参见此页面: http://docs.mongodb .org/manual/reference/mongodb-extended-json/
See this page for the different formats: http://docs.mongodb.org/manual/reference/mongodb-extended-json/
如果查询未找到任何文档(并且您确定它们在集合中存在),则还有其他问题.我将仔细检查您要连接的服务器,并首先检查数据库和集合的名称.
If the query is not finding any documents (and you are sure they are present in the collection) then there is some other issue. I would double check the server(s) you are connecting to and the name of the database and collection first.
Rob.
这篇关于如何使用Java使用ObjectIds创建查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!