我的文档看起来像这样:
{
"_id" : ObjectId("5a5081321d5e182e14f20589"),
"building_number" : "01",
"floor_1" : {
"roomA" : {
"occupant" : "vacant",
"department" : "science",
"space_code" : "103",
"space_type" : "office",
"furniture_assets" : {
"number_1" : {
"barcode" : "8076809513456",
"description" : "Metal Desk",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_2" : {
"barcode" : "9780444505156",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_3" : {
"barcode" : "5209533250890",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_4" : {
"barcode" : "9303939104545",
"description" : "Gas-lift Chair",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
}
}
},
"roomB" : {
"occupant" : "vacant",
"department" : "science",
"space_code" : "103",
"space_type" : "office",
"furniture_assets" : {
"number_1" : {
"barcode" : "7258029188960",
"description" : "Metal Desk",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_2" : {
"barcode" : "4219366494100",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_3" : {
"barcode" : "985355502553",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_4" : {
"barcode" : "930393910527",
"description" : "Gas-lift Chair",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_4" : {
"barcode" : "93039391033",
"description" : "Gas-lift Chair",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
}
}
},
"roomC" : {
"occupant" : "vacant",
"department" : "science",
"space_code" : "103",
"space_type" : "office",
"furniture_assets" : {
"number_1" : {
"barcode" : "7258021234960",
"description" : "Metal Desk",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_2" : {
"barcode" : "4219366741236",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_3" : {
"barcode" : "985355509513",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_4" : {
"barcode" : "987593910527",
"description" : "Gas-lift Chair",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
}
}
}
},
"floor_2" : {
"roomA" : {
"occupant" : "vacant",
"department" : "mathematics",
"space_code" : "105",
"space_type" : "office",
"furniture_assets" : {
"number_1" : {
"barcode" : "7776809513456",
"description" : "Metal Desk",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_2" : {
"barcode" : "9120444505156",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_3" : {
"barcode" : "4209533250890",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_4" : {
"barcode" : "9303939104545",
"description" : "Gas-lift Chair",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
}
}
},
"roomB" : {
"occupant" : "vacant",
"department" : "mathematics",
"space_code" : "105",
"space_type" : "office",
"furniture_assets" : {
"number_1" : {
"barcode" : "3658029188960",
"description" : "Metal Desk",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_2" : {
"barcode" : "2119366494100",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_3" : {
"barcode" : "495355502553",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_4" : {
"barcode" : "930393910527",
"description" : "Gas-lift Chair",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_4" : {
"barcode" : "46039391033",
"description" : "Gas-lift Chair",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
}
}
},
"roomC" : {
"occupant" : "vacant",
"department" : "mathematics",
"space_code" : "105",
"space_type" : "office",
"furniture_assets" : {
"number_1" : {
"barcode" : "7258074124960",
"description" : "Metal Desk",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_2" : {
"barcode" : "4219367896236",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_3" : {
"barcode" : "22535512313",
"description" : "Pine bookcase",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
},
"number_4" : {
"barcode" : "142593910527",
"description" : "Gas-lift Chair",
"acquisition_date" : "07/07/2008",
"cost" : "$100"
}
}
}
}
}
如何使用java驱动程序更改空闲对象?
我使用了下面的代码,但它似乎没有用。
DBCollection coll = db.getCollection("Facilities");
String buildingNum = (String) buildingBox.getValue();
String floorNum = (String) floorBox.getValue();
String roomNum = (String) roomBox.getValue();
BasicDBObject document = new BasicDBObject();
document.put("building_number", buildingNum);
DBCursor dbCursor = coll.find(document);
while(dbCursor.hasNext()) {
DBObject dbo = dbCursor.next();
//DBObject dbObject1 = (DBObject) (((DBObject) dbo.get(floorNum)).get(roomNum));
// String change = floorNum;
String email = userDetails.get(0);
BasicDBObject occupant = new BasicDBObject();
occupant.append("$set",new BasicDBObject().append( "occupant",email));
DBObject findRoom = (DBObject) ((DBObject) dbo.get(floorNum)).get(roomNum);
coll.update(findRoom,occupant,false,false);
即使没有错误,它也不会改变数据库中的值。那么如何更新价值"占用者"使用java驱动程序? 有没有办法改变价值?喜欢这个代码的改编?
答案 0 :(得分:0)
由于您获得了楼层和房间号码,因此您可以将文档与建筑物编号匹配,并将更新密钥与楼层和房间号码连接以更新占用者字段
String buildingNum = "01";
String floorNum = "1";
String roomNum = "A";
Bson filter = Filters.eq("building_number", buildingNum);
String key = "floor_" + floorNum + ".room" + roomNum + ".occupant";
Document update = new Document("$set", new Document(key, "occupied"));
UpdateOptions options = new UpdateOptions().upsert(false);
UpdateResult updateResult = coll.updateOne(filter, update, options); // change to many if required
System.out.println(updateResult.getMatchedCount() + " " + updateResult.getModifiedCount() + " " + updateResult.getUpsertedId());
系统输出
1 1 null
mongo collection
> db.Facilities.find({"building_number":"01"},{"floor_2.roomA.occupant" : 1})
{ "_id" : ObjectId("5a51b6c6baf48d5ef9db9f38"), "floor_2" : { "roomA" : { "occupant" : "vacant" } } }
> db.Facilities.find({"building_number":"01"},{"floor_1.roomA.occupant" : 1})
{ "_id" : ObjectId("5a51b6c6baf48d5ef9db9f38"), "floor_1" : { "roomA" : { "occupant" : "occupied" } } } // updated with occupied
>
修改强>
BasicDBObject
DBCollection coll = db.getCollection("Facilities");
String buildingNum = (String) buildingBox.getValue();
String floorNum = (String) floorBox.getValue();
String roomNum = (String) roomBox.getValue();
String key = "floor_" + floorNum + ".room" + roomNum + ".occupant";
BasicDBObject docFilter = new BasicDBObject("building_number", buildingNum);
BasicDBObject docUpdate = new BasicDBObject("$set",
new BasicDBObject(key, "occupied"));
WriteResult updateResult = coll.update(docFilter, docUpdate);