简体   繁体   English

Mongo/Node JS,如何更新数据库中的记录?

[英]Mongo/Node JS , How Updating records in the database?

I have a feature in my program where in there is a job that will get file every 2 hours and parse to json and then add those data to the database.我的程序中有一个功能,其中有一个作业将每 2 小时获取一次文件并解析为 json,然后将这些数据添加到数据库中。

Using upsert I was able to create record if no match and then update records that matches, there is no problem with this.如果不匹配,我可以使用 upsert 创建记录,然后更新匹配的记录,这没有问题。

The file i am getting every 2 hours has dynamic values where it the total data depends, sometimes some data is reduced, sometimes new data is added.我每 2 小时获取的文件具有动态值,它取决于总数据,有时会减少一些数据,有时会添加新数据。

So for example first 2 hours the file contains 3 records I was able to add 3 records to the database and the database now has 3 current records.因此,例如前 2 小时文件包含 3 条记录,我能够将 3 条记录添加到数据库中,并且数据库现在有 3 条当前记录。

Current Record in the database数据库中的当前记录

value: {
    "userId": 1,
        "id": 1,
            "title": "delectus aut autem",
                "completed": false
},
"userId": 2,
    "id": 2,
        "title": "delectus aut autem2",
            "completed": false
  },

"userId": 3,
    "id": 3,
        "title": "delectus aut autem3",
            "completed": false

}

But on the next 2 hours there are only 2 records from a file one was removed.但在接下来的 2 小时内,一个文件中只有 2 条记录被删除。 So since there is match it will update the 2 records因此,由于匹配,它将更新 2 条记录

JSON File Data JSON 文件数据

 value: {
        "userId": 1,
            "id": 1,
                "title": "delectus aut autem",
                    "completed": false
    },
    "userId": 2,
        "id": 2,
            "title": "delectus aut autem2",
                "completed": false
      },

It would also update all record that was removed in the file but present in the databse set completed == true它还将更新文件中已删除但存在于数据库集中的所有记录已完成 == true

So the final record in the databsae would be like this所以数据库中的最终记录是这样的

Userid3 completed value would be now equal to true. Userid3 完成值现在将等于 true。

 value: {
        "userId": 1,
            "id": 1,
                "title": "delectus aut autem",
                    "completed": false
    },
    "userId": 2,
        "id": 2,
            "title": "delectus aut autem2",
                "completed": false
      },

    "userId": 3,
        "id": 3,
            "title": "delectus aut autem3",
                "completed": true

    }

My Query我的查询

var myQuery = { 'id': value.id }
//Update existing data ang create if no data found using upsert
Person.model.findOneAndUpdate(myQuery, value, { upsert: true }, function (err, doc) {
    if (err) {
        console.log("[ERROR]", err)
    }
})

findOneandUpdate won't work like that if you want to update multiple documents.如果您想更新多个文档,findOneandUpdate 将不会那样工作。 You should be using bulk update for that.您应该为此使用批量更新。 Here is the link这是链接

I hope it helps.我希望它有所帮助。 Thanks谢谢

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

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