所以我有这样一个蒙戈文件,我需要根据VAL更新嵌套阵列文档中的MongoDB
{
"_id" : NumberLong(222),
"pattern":"grain"
"BASIC" : {
"frame":"clear"
"tin" : [
{
"val" : "abc",
"unit" : NumberLong(2311)
},
{
"val" : "def",
"unit" : NumberLong(2311)
},
]
}
}
这里刷新阵列是我试过
collection = db.getCollection("test");
Bson where = new Document().append("_id", 222).append("BASIC.tin.val","abc");
Bson update = new Document()
.append("BASIC.tin.$.val", "xyz");
Bson set = new Document().append("$set", update);
try {
UpdateResult result = collection.updateOne(where , set, new UpdateOptions().upsert(true));
if(result.getMatchedCount()>0){
System.out.println("updated");
System.out.println(result.getModifiedCount());
}else{
System.out.println("failed");
}
} catch (MongoWriteException e) {
e.printStackTrace();
}
代码更新工作正常,但不会upsert,如果发现失败 这是我得到的错误:
com.mongodb.MongoWriteException:位置运算符没有找到所需的匹配RY。未展开的更新:。BASIC.tin $ VAL 在com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:558) 在com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:542)
你想UPSERT到嵌入文档 “锡”? – Veeram