首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongodb:如何使用mongodb驱动程序更改嵌套文档的值

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

Stack Overflow用户
提问于 2018-01-07 05:06:36
回答 1查看 1.1K关注 0票数 1

我的文档如下所示:

代码语言:javascript
复制
{
"_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驱动程序更改空对象?

我使用了下面的代码,但似乎不起作用。

代码语言:javascript
复制
  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驱动程序更新“占用者”值呢?有办法改变价值吗?就像这段代码的改编?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-07 06:21:33

由于您正在获取楼层和房间号,因此可以将文档与建筑物编号相匹配,并将更新密钥与楼层和房间号连接起来,以便更新占用人字段。

代码语言:javascript
复制
    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());

系统,系统

代码语言:javascript
复制
1 1 null

蒙古族藏品

代码语言:javascript
复制
> 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

代码语言:javascript
复制
    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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48134502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档