【问题标题】:Update nested array list in mongodb java在 mongodb java 中更新嵌套数组列表
【发布时间】: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


    【解决方案1】:

    您在$push 中的“nameIdentity”字段之后错过了positional $ 运算符:

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

    $push 操作与其他更新操作修饰符一样需要知道要处理的匹配数组元素的“索引”。否则会出现您报告的错误。

    【讨论】:

    • 我的 nameIdentity 中有多个名称,即使匹配的文档是第二个或第三个,sourceId 总是被附加到第一个文档。如何更新匹配的文档。
    • @ShaikMujahidAli 位置 $ 运算符(如链接的手册页中所述)不返回任何匹配索引,但匹配的“第一次”出现总是意味着“外部”大批。在这种情况下,您只需要“外部索引”匹配,就可以了。这将附加到该数组匹配的任何查询条件的匹配索引。在您的情况下,无论名称是什么。但是如果你有不止一次“JOY”,那么只有第一个匹配。但是,您提出的问题根本没有使用位置 $ 运算符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2017-09-11
    相关资源
    最近更新 更多