简体   繁体   English

如何在mongo中更新数据

[英]How do I update data in mongo

So I am currently working on a project with mongodb and nodejs and I was wondering, how can you update data in mongodb via nodejs? 所以我目前正在使用mongodb和nodejs进行项目,我想知道如何通过nodejs更新mongodb中的数据? My problem is that I want to keep the old data and add new. 我的问题是我想保留旧数据并添加新数据。 For example, here is the data currently in my mongodb 例如,这是我mongodb中当前的数据

{
    "_id" : ObjectId("5a1c0c1c3b147ec2e31cceb3"), 
    "event_id" : "1", 
    "event_medium" : "null", 
    "event_tags" : ["#JustTesting"]
}

So I want to add new data to the event_tags array and still keep the old data. 所以我想将新数据添加到event_tags数组中,并且仍然保留旧数据。

So for example the end result would be this: 因此,例如最终结果将是这样的:

{
    "_id" : ObjectId("5a1c0c1c3b147ec2e31cceb3"), 
    "event_id" : "1",
    "event_medium" : "null", 
    "event_tags" : ["#JustTesting", "#Test", "#Something"]
}

Your starting point is the Update function in CRUD (Create,Read, Update, Delete) operations in Mongodb. 您的起点是Mongodb中CRUD(创建,读取,更新,删除)操作中的Update函数。

Your node program should have among others the update function where you set the _id field you want to update and load the content fields to be update in 'data' for example as below: 您的节点程序应具有更新功能,其中您可以设置要更新的_id字段,并在“数据”中加载要更新的内容字段,例如:

myModel.prototype.update = function (_id, data, callback) {
    const query = { _id: this.mongo.ObjectId(_id) };
    debug(' update:' + JSON.stringify(query));
    this.mongo.collection('mycollection').update(query, data, callback);
};

This piece of code should be put in your Model, if you use MVC pattern. 如果使用MVC模式,则应将这段代码放入模型中。

There is a lot to go. 还有很多事情要做。 Honestly I recommend a more deep tutorial like parts 3 and 4 of this one for nodejs and Mongoose (mongo db driver): MDN tutorial for mongo/node/express 老实说,我为Nodejs和Mongoose(mongo db驱动程序)推荐了更深入的教程,如本教程的第3部分和第4部分: mongo / node / express的MDN教程

I assume you are using mongoose.. 我以为你在用猫鼬。

    eventModel.findOne({event_id:1 },
        function(err, eventObj){
        if(err){
          //handle error
        } else { 
          if(eventObj === null) {
            //event doesnot exist       
          }
          var tagList = eventObj.event_tags; 
          tagList.push('new_tag1');
          tagList.push('new_tag2');
          eventObj.event_tags = tagList;
          eventObj.save(function(err){
            if(err){
              //handle error
            } else {
              //success
            }
          })

You should use the update function of MongoDB for that. 您应该为此使用MongoDB的更新功能。 MongoDB knows different update operators , in your case you may use $push or $pushAll (the second is deprecated): MongoDB知道不同的更新运算符 ,在您的情况下,您可以使用$push$pushAll (不建议使用第二个):

update one after the other with $push $push一个接一个地更新

YourCollection.update({ _id: 'xxx' }, { $push: { event_tags: '#Test' } });
YourCollection.update({ _id: 'xxx' }, { $push: { event_tags: '#Something' } });

or both at once with $pushAll (deprecated now) 或同时与$pushAll (立即弃用)

YourCollection.update({ _id: 'xxx' }, { $pushAll: { event_tags: ['#Test', '#Something'] } });

To interact with MongoDB form your NodeJS app, I would use a library like this one . 为了从您的NodeJS应用程序与MongoDB进行交互,我将使用像这样的库。

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

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