Java — MongoDB-collection.finden() durch _id

Ich bin versucht, ein element aus einer Sammlung, die unter Verwendung seines eindeutigen _id, aber ich kann nicht finden wie.

Dies ist mein code

MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("DB");
MongoCollection<Document> collection =   database.getCollection("COLL");

Wenn ich die Abfrage für meine db mit

BasicDBObject query=new BasicDBObject("info.i0","0");
Document myDoc = collection.find(query).first();
System.out.println(myDoc.toJson());

Bekomme ich als Ausgabe

{ "_id" : { "$oid" : "560ea3f205240f065a3e9d19" }, "name" : "MongoDB", "type" : "database", "count" : 1, "info" : { "i0" : "0", "i1" : "1", "i2" : "2", "i3" : "3", "i4" : "4", "i5" : "5", "i6" : "6", "i7" : "7", "i8" : "8", "i9" : "9" } }

Aber Wenn ich versuche

BasicDBObject query=new BasicDBObject("_id.$oid","560ea3f205240f065a3e9d19");
Document myDoc = collection.find(query).first();
System.out.println(myDoc.toJson());

Bekomme ich eine Null-pointer-exception, myDoc null ist.

Was mache ich falsch?

InformationsquelleAutor rastabob | 2015-10-02
Schreibe einen Kommentar