gpt4 book ai didi

java - Mongodb:如何使用 mongodb java 驱动程序更改嵌套文档的值

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:18 25 4
gpt4 key购买 nike

我的文档看起来像这样:

{
"_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 驱动程序更新“占用者”值呢?有没有办法改变这个值?喜欢此代码的改编吗?

最佳答案

由于您获取了楼层和房间号,因此您可以将文档与建筑物号相匹配,并将更新 key 与楼层和房间号连接起来以更新占用者字段

    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

蒙戈集合

> 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);

关于java - Mongodb:如何使用 mongodb java 驱动程序更改嵌套文档的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com