簡體   English   中英

如何使用 Spring 在 mongodb 文檔的嵌套數組中添加 json?

[英]How to add a json in a nested array of a mongodb document using Spring?

存儲在mongodb中的文檔:


{
"CNF_SERVICE_ID":"1",
"SERVICE_CATEGORY":"COMMON_SERVICE",
"SERVICES":[{
    "SERVICE_NAME":"Authentication Service",
    "VERSIONS":[{
            "VERSION_NAME":"AuthenticationServiceV6_3",
            "VERSION_NUMBER":"2",
            "VERSION_NOTES":"test",
            "RELEASE_DATE":"21-02-2020",
            "OBSOLETE_DATE":"21-02-2020",
            "STATUS":"Y",
            "GROUPS":[{
                "GROUP_NAME":"TEST GROUP",
                "CREATED_DATE":"",
                "NODE_NAMES":[
                    ""
                    ],
                "CUSTOMERS":[{
                    "CUSTOMER_CONFIG_ID":"4",
                    "ACTIVATION_DATE":"21-02-2020",
                    "DEACTIVATION_DATE":"21-02-2020",
                    "STATUS":"Y"
                }]
            }]
        }]
    }
    ]
}

現在,我需要將另一個客戶 json 添加到上面同一文檔中“GROUPS”內的數組“CUSTOMERS”中。 客戶 json 將是這樣的:

{
    "CUSTOMER_CONFIG_ID":"10",
    "ACTIVATION_DATE":"16-03-2020",
    "DEACTIVATION_DATE":"16-03-2021",
    "STATUS":"Y"
}

我試過這個:


Update update = new Update().push("SERVICES.$.VERSIONS.GROUPS.CUSTOMERS",customerdto);
mongoOperations.update(query, update, Myclass.class, "mycollection");


但是,我收到了異常: org.springframework.data.mongodb.UncategorizedMongoDbException: 命令失敗,錯誤 28 (PathNotViable): 'Cannot create field 'GROUPS' in element


[編輯添加]

我能夠使用過濾的位置運算符更新它。 以下是我使用的查詢:

update(
 {"SERVICE_CATEGORY":"COMMON_SERVICE"},
 {"SERVICES.SERVICE_NAME":"Authentication Service"},
 {"SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"}
 {
    $push:{"SERVICES.$[service].VERSIONS.$[version].GROUPS.$[group].CUSTOMERS":{
        "CUSTOMER_CONFIG_ID":"6",
        "ACTIVATION_DATE":"31-03-2020",
        "STATUS":"Y"
    }
    }
 },
 {
        multi: true,
        arrayFilters: [ { $and:[{ "version.VERSION_NAME": "AuthenticationServiceV6_3"},{"service.SERVICE_NAME":"Authentication Service"},{"group.GROUP_NAME":"TEST GROUP"}]} ]
    }
 );

實際上,無論過濾條件如何,此查詢都會更新所有字段。 所以。 我試過這個,但我面臨語法異常。 請幫忙。

 update( {"SERVICE_CATEGORY":"COMMON_SERVICE"}, {"SERVICES.SERVICE_NAME":"Authentication Service"}, {"SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"} { $push:{"SERVICES.$[service].VERSIONS.$[version].GROUPS.$[group].CUSTOMERS":{ "CUSTOMER_CONFIG_ID":"6", "ACTIVATION_DATE":"31-03-2020", "STATUS":"Y" } } }, { multi: true, arrayFilters: [ { $and:[{ "version.VERSION_NAME": "AuthenticationServiceV6_3"},{"service.SERVICE_NAME":"Authentication Service"},{"group.GROUP_NAME":"TEST GROUP"}]} ] } );

更新:2020 年 4 月 1 日

我試過的代碼:

 validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name())); Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("s.SERVICE_NAME").is(servicedetail.getService_name()).and("v.VERSION_NAME").is(version.getVersion_name())); mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);

拋出以下異常:

錯誤 com.sample.amt.mongoTemplate.MongoOperations - count(query, collectionName,key,env) 中的異常:: org.springframework.dao.DataIntegrityViolationException:解析數組過濾器時出錯:: 由:: 預期單個頂級字段引起名稱,找到“SERVICE_CATEGORY”和“s”; 嵌套異常是 com.mongodb.MongoWriteException: Error parsing array filter :: 由 :: 導致 :: 期望一個頂級字段名稱,找到“SERVICE_CATEGORY”和“s”

此更新查詢根據指定的過濾條件將 JSON 添加到嵌套數組"SERVICES.VERSIONS.GROUPS.CUSTOMERS" 請注意,您的過濾條件將更新操作定向到(嵌套數組的)特定數組。

// JSON document to be added to the CUSTOMERS array
new_cust = { 
             "CUSTOMER_CONFIG_ID": "6", 
             "ACTIVATION_DATE": "31-03-2020", 
             "STATUS": "Y" 
}

db.collection.update( 
  { 
      "SERVICE_CATEGORY": "COMMON_SERVICE", 
      "SERVICES.SERVICE_NAME": "Authentication Service",
      "SERVICES.VERSIONS.VERSION_NAME": "AuthenticationServiceV6_3"
  }, 
  { 
      $push: { "SERVICES.$[s].VERSIONS.$[v].GROUPS.$[g].CUSTOMERS": new_cust } 
  },
  {
      multi: true,
      arrayFilters: [
          { "s.SERVICE_NAME": "Authentication Service" },
          { "v.VERSION_NAME": "AuthenticationServiceV6_3" },
          { "g.GROUP_NAME": "TEST GROUP" }
      ]
  }
);

更新具有多於一層嵌套的嵌套數組的文檔時,需要注意的幾件事。

  • 使用全位置運算符$[]和過濾位置運算符$[<identifier>]不是$位置運算符。 使用過濾位置運算符使用arrayFilters參數指定數組過濾條件。 請注意,這會將您的更新定向到特定的嵌套數組。
  • 對於過濾的位置運算符$[<identifier>] ,標識符必須以小寫字母開頭並且僅包含字母數字字符。

參考:

感謝@prasad_ 提供查詢。 我最終能夠使用 Spring 數據 MongoTemplate 的 updateMulti 方法成功地將查詢轉換為代碼。 我已經發布了下面的代碼:

Query validationquery = new Query();
                        validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name()));

Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("s.SERVICE_NAME").is(servicedetail.getService_name())).filterArray(Criteria.where("v.VERSION_NAME").is(version.getVersion_name()));    
                            mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
mongoTemplateobj.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM