【发布时间】:2015-08-24 17:59:38
【问题描述】:
大家好,我有一个像这样的集合` "_id" : ObjectId("55dabba974cd60712be24443"),
"entityType" : "1",
"entityCreatedDate" : "08/24/2015 12:07:20 PM",
"nameIdentity" : [
{
"givenNameOne" : "JOY",
"givenNameThree" : "BRAKEL",
"lastName" : "BRAKEL",
"createdDate" : "08/24/2015 12:07:20 PM",
"sourceId" : [
{
"sourceId" : "55dabba974cd60712be24441"
}
]
},
],
Here name identity is a list as well as sourceId. I am trying to update sourceId list in nameIdentityList if it matches the names. My java code is :
Document sourceDocument=new Document("sourceId",sourceId);
mongoDatabase.getCollection("entity").updateOne(new Document("entityId", entityId).append("nameIdentity.givenNameOne","JOY"),
new Document("$push", new Document("nameIdentity.sourceId", sourceDocument)));
` 但是我得到了类似 java.lang.RuntimeException: com.mongodb.MongoWriteException: cannot use the part (nameIdentity of nameIdentity.sourceId) to traverse the element ({nameIdentity.
如果我的条件得到满足,我希望是这样的:
`"_id" : ObjectId("55dabba974cd60712be24443"),
"entityType" : "1",
"entityCreatedDate" : "08/24/2015 12:07:20 PM",
"nameIdentity" : [
{
"givenNameOne" : "JOY",
"givenNameThree" : "BRAKEL",
"lastName" : "BRAKEL",
"createdDate" : "08/24/2015 12:07:20 PM",
"sourceId" : [
{
"sourceId" : "55dabba974cd60712be24441"
},
{
"sourceId" : "55dabba974cd60712be24435"
}
]
},
],`
。有什么建议我哪里出错了吗? 我的 nameIdentity 中有多个名称,即使匹配的文档是第二个或第三个,sourceId 总是被附加到第一个文档。我如何更新到特定的匹配文档。
【问题讨论】:
标签: java mongodb mongodb-query