如何在Java中更新嵌套的mongodb文档数据

时间:2015-02-26 10:29:30

标签: java mongodb

我的收藏中有一份文件,如下所示。我想更新特定的对象值。请解决这个问题。如何更新isChecked : "0"

    {
    "STUDENT_001": {
    "name": "name1",
    "hobbies": {
        "01": {
            "isChecked": "1",
            "name": "hobby - name"
        },
        "02": {
            "isChecked": "0",
            "name": "hobby-name"
        }
    }
    },
    "STUDENT_002": {
    "name": "name2",
    "hobbies": {
        "01": {
            "isChecked": "1",
            "name": "hobby - name"
        },
        "02": {
            "isChecked": "1",
            "name": "hobby-name"
        }
    }
    }
}

1 个答案:

答案 0 :(得分:0)

应该是这样的:

.update(
  query,
  new BasicDBObject(
    "$set",
    new BasicDBObject("STUDENT_001.hobbies.01.isChecked", "0")
));

这里查询匹配文档。并且$set更新指定路径中的字段,即"STUDENT_001.hobbies.01.isChecked"