简体   繁体   English

更新 JSON Object 在 Mongodb 使用 Z2A2D595E6ED9A0B3B34F027F2B6D

[英]Updating JSON Object in Mongodb using spring boot

Suppose I have a JSON Object which needs to be updated in Mongodb like假设我有一个 JSON Object 需要在 Mongodb 中更新

{
  "_id": 12345,
  "Attribute": { "Property1": "X", "Property2": true, "Property3": 123 }
}

Suppose I have a record in mongoDb假设我在 mongoDb 有记录

{
    "_id": 12345,
    "Attribute1": "abc",
    "Attribute2": "xyz",
    "Attribute": { "Property4": "X", "Property2": false, "Property3": 456 }
}

The resultant should update Attribute JSON while updating only the fields that are changed and keeping rest of the values intact.结果应更新属性 JSON,同时仅更新已更改的字段并保持 rest 的值不变。 Resultant record in db should be like this db 中的结果记录应该是这样的

{
    "_id": 12345,
    "Attribute1": "abc",
    "Attribute2": "xyz",
    "Attribute": { "Property4": "X", "Property1": "X", "Property2": true, "Property3": 123 }
}

I really don't know how to achieve this in single Pass in Mongodb using JAVA spring boot.我真的不知道如何在 Mongodb 中使用 JAVA spring 引导来实现这一点。 Can Anyone please help?有人可以帮忙吗? Any help is appreciated.任何帮助表示赞赏。

You can use Update class from org.springframework.data.mongodb.core.query package.您可以使用来自 org.springframework.data.mongodb.core.query package 的更新 class。 You can write a code snippet like below.您可以编写如下代码片段。

Update updateAttribute = new Update();
      updateAttribute.push("Attribute", "Your Value");
      mongoOperations.updateFirst(new Query(Criteria.where("id").is(12345)), updateAttribute, "yourCollection");

Also, you need to inject constructor for MongoOperations from org.springframework.data.mongodb.core package.此外,您需要从 org.springframework.data.mongodb.core package 为 MongoOperations 注入构造函数。

You can do it in 2 ways,您可以通过 2 种方式进行操作,

  1. For Mongo version 4.4+ you can use pipelined updates , this allows the use of aggregation operators in the update body, specifically we'll want to use $mergeObjects , like so:对于 Mongo 4.4+ 版本,您可以使用流水线更新,这允许在更新主体中使用聚合运算符,特别是我们将要使用$mergeObjects ,如下所示:
db.collection.update(
{
  _id: 12345
},
[
  {
    $set: {
      Attribute: {
        $mergeObjects: [
          "$Attribute",
          {
            "Property1": "X",
            "Property2": true,
            "Property3": 123
          }
        ]
      }
    }
  }
])

Mongo Playground蒙戈游乐场

  1. For lesser Mongo versions you'll have to construct the update body in code, here is a javascript example ( might be slightly more annoying in spring )对于较小的 Mongo 版本,您必须在代码中构建更新主体,这是一个 javascript 示例(在 spring 中可能会更烦人)
const input = {
    '_id': 12345,
    'Attribute': { 'Property1': 'X', 'Property2': true, 'Property3': 123 },
};

const updateBody = {};
Object.keys(input.Attribute).forEach((key) => {
    const updateKey = `Attribute.${key}`;
    updateBody[updateKey] = input.Attribute[key];
});
db.collection.updateOne({ _id: 12345 }, { $set: updateBody });

By using the dot notation in the update body we ensure we don't overwrite existing fields in the Attribute .通过在更新正文中使用点表示法,我们确保不会覆盖Attribute中的现有字段。

I achieved this using this query.我使用这个查询实现了这一点。

db.collection.update(
    {
      _id: 12345
    },
      {
        $set: {
              
                "Property1": "X", 
                "Property2": true, 
                "Property3": 123 
               }
            
      },
      {upsert: true}
     )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM