简体   繁体   English

如何在mongodb中仅更新新数据或更改的值?

[英]How can I update only new data or changed value in mongodb?

Update command updates the key with provided Json. Update命令使用提供的Json更新密钥。 I want to update only the object that is not present in db and changed value. 我只想更新数据库中不存在的对象和更改的值。 How can I do that? 我怎样才能做到这一点?

"data" : [ 
    {
        "_id" : "5bb6253d861d057857ec3ff0",
        "name" : "C"
    }, 
    {
        "_id" : "5bb625fc861d057857ec3ff1",
        "name" : "B"
    }, 
    {
        "_id" : "5bb625fe861d057857ec3ff2",
        "name" : "A"
    }
]

my data is like this. 我的数据是这样的。 So, if one more array object comes in json of only 2 new object comes then it should insert the two data along with the 3 data. 因此,如果只有2个新对象的json中有一个数组对象,则它应将2个数据与3个数据一起插入。

Update the object that is not present in DB: 更新数据库中不存在的对象:

Use upsert : Upsert creates a new document when no document matches the query criteria. 使用upsert :当没有文档符合查询条件时,Upsert将创建一个新文档。 Alternatively, you can add null checks in your query eg { user_id:null }. 或者,您可以在查询中添加空检查,例如{user_id:null}。 This will allow to update the data where a record for the user is not present in DB. 这将允许更新数据库中不存在用户记录的数据。

Update changed value: 更新更改的值:

This can be implemented maintaining a key to store last_updated_at. 这可以通过维护存储last_updated_at的密钥来实现。 If the last_updated_at value does not match to the previously_updatede_at that record can be treated at modified 如果last_updated_at值与previous_updatede_at不匹配,则可以修改该记录

You can implement Change Streams, introduced in MongoDB 3.6 from which you can receive real time changes on your data. 您可以实施MongoDB 3.6中引入的变更流,您可以从中接收数据的实时变更。 You can receive only the data that was changed, filtering by the "update" operation. 您只能接收通过“更新”操作过滤的已更改数据。 Furthermore you can also filter for data that is newly inserted filtering by the "insert" operation. 此外,您还可以通过“插入”操作过滤新插入的数据。 Please see Change Streams . 请参阅更改流

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

相关问题 如何在数据更改时更新MongoDB文档? - How to update MongoDB document only when data changed? 嗨,如何跳过未更改值的更新,并对 MongoDB 和 nodejs 中的更改值执行更新操作 - Hi ,how can skip the update of unchanged value and will perform update operation on changed value in MongoDB and nodejs Mongodb:仅当我要上传的值不为空时如何更新字段? - Mongodb: how to update a field only if the value I want to upload is not null? 如何更新mongodb文档以向阵列添加新项? - How can I update mongodb document for adding a new item to array? 如何仅更新已更改的属性 - Spring MVC mongoDB - How to update only attributes that have changed - Spring MVC mongoDB 如何使用新值更新MongoDB中的字段 - How to update a field in MongoDB with new value 如何使用 mongoDB 更新 prisma 中已存储的布尔值? - How can I update an already stored boolean value in prisma with mongoDB? 如何在mongodb中的现有记录中插入新值? - how can i insert new value to existing record in mongodb? 如何在mongodb中使用不同的数据更新某些文档 - How can I update some documents with different data in mongodb 如何使用 Spring Data MongoDB 插入新日期? - How can I do a insert a new Date with Spring Data MongoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM