0

Java と mongoDB を使用しています。私は次のjsonを持っています、

{ "_id" : { "$oid" : "524a27a318c533dc95edafe1"} , "RoomNumber" : 516 , "RoomType" : "presidential" , "Reserved" : true , "RegularRate" : 400.0 , "Discount" : [ 0.85 , 0.75 , 1.0 , 1.0] , "割引率" : 0}

{ "_id" : { "$oid" : "524a27a318c533dc95edafe2"} , "RoomNumber" : 602 , "RoomType" : "presidential" , "Reserved" : false , "RegularRate" : 500.0 , "Discount" : [ 1 , 0.75 , 1.0 , 1.0] , "割引率" : 0}

{ "_id" : { "$oid" : "524a27a318c533dc95edafe3"} , "RoomNumber" : 1315 , "RoomType" : "Single" , "Reserved" : true , "RegularRate" : 100.0 , "Discount" : [ 1 , 1 , 1.0 , 1.0] , "割引率" : 0}

コレクション内のドキュメントには異なる部屋番号があります。部屋番号がわかっている場合、その部屋番号を含むドキュメントを取得し、そのドキュメント内の他のすべての値を取得するにはどうすればよいですか。

たとえば、602 の場合、roomtype: プレジデンシャル、reserved: false、Regular rate: 500 を取得できるようにしたい

ありがとう

4

1 に答える 1

0

そのためのシェル クエリは次のようになります。

db.rooms.findOne({RoomNumber: 602}, {RoomType: 1, Reserved: 1, RegularRate:1, _id: 0});

ネイティブ mongo ドライバーを使用する場合:

BasicDBObject query = new BasicDBObject("RoomNumber", 602);
BasicDBObject projection = new BasicDBObject("RoomType", 1);
projection
.append("Reserved", 1)
.append("RegularRate", 1)
.append("_id", 0);
DBObject result = collection.findOne(query, projection);
于 2013-10-02T04:47:54.493 に答える