ここ機能PROGだ3.5ドライバに対してコンパイル
MongoClient mongoClient = new MongoClient();
MongoDatabase db = mongoClient.getDatabase("testX");
MongoCollection<BsonDocument> coll = db.getCollection("foo", BsonDocument.class);
coll.drop();
{
BsonDocument doc = new BsonDocument();
doc.put("from", new BsonInt32(23343223));
doc.put("to", new BsonInt32(23343223));
doc.put("msg", new BsonString("hello"));
coll.insertOne(doc);
doc.remove("_id");
doc.put("from", new BsonInt32(8889));
doc.put("to", new BsonInt32(99999));
doc.put("msg", new BsonString("goodbye"));
coll.insertOne(doc);
}
{
BsonDocument query = new BsonDocument("from", new BsonInt32(8889));
BsonDocument proj = new BsonDocument("msg", new BsonInt32(1));
proj.put("_id",new BsonInt32(0));
BsonDocument d2 = coll.find(query).projection(proj).first();
System.out.println(d2);
String s2 = coll.find(query).projection(proj).first().getString("msg").getValue();
System.out.println(s2);
}
。
ご質問がある場合はお手伝いできません混乱、入出力はなく、努力の兆候はありません –
[mongoose .find()]のreturn document \ _idの複製が可能です(https://stackoverflow.com/questions/46284659/only-return-document-id -on-mongoose-find)である。 [Projection](https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection)を使用します。私は私の答えでそれを使用する方法について少し説明します。 – zero298
@ zero298私はあなたの実装を行いましたが、動作しませんでした。あるいはmongoDBを初めて使用しているため誤って実装されていたかもしれません。 "await"でエラーが発生しました。ありがとう。 – Ratik