![](/img/trans.png)
[英]How to query documents using “_id” field in Java mongodb driver without using the collection name?
[英]How to query documents using "_id" field in Java mongodb driver?
我試圖通過搜索“_id”鍵在 MongoDB 中查找文檔。 我的文件看起來像這樣-
{
"_id" : ObjectId("4f693d40e4b04cde19f17205"),
"hostname" : "hostnameGoesHere",
"OSType" : "OSTypeGoesHere"
}
我正在嘗試將此文檔搜索為-
ObjectId id= new ObjectId("4f693d40e4b04cde19f17205");
BasicDBObject obj = new BasicDBObject();
obj.append("_id", id);
BasicDBObject query = new BasicDBObject();
query.putAll(query);
但我得到以下錯誤 -
error: reference to putAll is ambiguous, both method putAll(Map) in BasicBSONObject and method putAll(BSONObject) in BasicBSONObject match
query.putAll(query);
BasicDBObject 的 append 方法支持 (String Key, Value) ,如果我將“_id”作為字符串傳遞給此方法,則沒有匹配的文檔。
所以我的問題是如何傳遞“_id”?
不確定其他人是否可能正在尋找有關此主題的答案,但這是基於“_id”搜索 MongoDB 記錄的最簡單方法。 MongoDB 文檔未更新,但仍將 ObjectId 顯示為com.mongodb
包的一部分(它通常也沒有提供有關按 ObjectId 搜索的大量信息)。
import org.bson.types.ObjectId;
public DBObject findDocumentById(String id) {
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
DBObject dbObj = collection.findOne(query);
return dbObj;
}
對於那些尋求更新方法的人,尤其是 3.4:
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.ObjectId;
import static com.mongodb.client.model.Filters.eq;
//......
MongoCollection<Document> myCollection = database.getCollection("myCollection");
Document document = myCollection.find(eq("_id", new ObjectId("4f693d40e4b04cde19f17205"))).first();
if (document == null) {
//Document does not exist
} else {
//We found the document
}
你可以這樣做
ObjectId id= new ObjectId("4f693d40e4b04cde19f17205");
BasicDBObject obj = new BasicDBObject();
obj.append("_id", id);
BasicDBObject query = new BasicDBObject();
query.putAll((BSONObject)query);
通過使用查詢作為解決它 -
query.putAll((BSONObject)query);
更簡單的方法是:
Bson filter = Filters.eq("_id", new ObjectId("4f693d40e4b04cde19f17205"));
mongoConfig.getMongoTemplate().getCollection("myCollection").find(filter);
我找到了一種方法,使用更多的代碼,需要一個表示要找到的 id 的字符串作為參數傳遞。
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import org.bson.conversions.Bson;
import org.bson.Document;
import org.bson.types.ObjectId;
public static void findCustomerById(String id) {
try (MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017")) {
MongoDatabase database = mongoClient.getDatabase("database_name");
Bson filter = Filters.eq("_id", new ObjectId(id));
MongoCollection<Document> collection = database.getCollection("customer");
//Tries to find the document
Document doc = collection. Find(filter).first();
//prints the document
System.out.println(doc.toJson());
} catch (Exception e) {
System.out.println("Error finding document");
System.out.println("Error in: " + e.getMessage());
e.printStackTrace();
}
你可以試試這個片段:
ObjectId id= new ObjectId("4f693d40e4b04cde19f17205");
BasicDBObject obj = new BasicDBObject();
obj.append("_id", id);
BasicDBObject query = new BasicDBObject();
query.putAll((BSONObject)obj);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.