最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Updating a mongodb document with JSON object - Stack Overflow

programmeradmin1浏览0评论

I want to update a MongoDB docment (using the Javascript db driver). I would like to pass in a JSON object and have that update the document ... something like:

Provider.prototype.updateProfile = function(username, profile, callback) {
  this.getCollection(function(error, profile_collection) {
    if( error ) callback( error );
    else {
      profile_collection.update(
        {username: username},
        {profile},
        function(error, profile){
          if( error ) callback(error);
          else callback(null, profile)
        });
    }
  });
};

I want this to be a generic function, so that if the document structure changes I don`t have to re-write. At the moment I can only get this working by using

{"$set": {x:profile.x, y:profile.y}}

in the update, does anyone have a generic solutions so that I can pass in any profile:

profile = { .... }

I want to update a MongoDB docment (using the Javascript db driver). I would like to pass in a JSON object and have that update the document ... something like:

Provider.prototype.updateProfile = function(username, profile, callback) {
  this.getCollection(function(error, profile_collection) {
    if( error ) callback( error );
    else {
      profile_collection.update(
        {username: username},
        {profile},
        function(error, profile){
          if( error ) callback(error);
          else callback(null, profile)
        });
    }
  });
};

I want this to be a generic function, so that if the document structure changes I don`t have to re-write. At the moment I can only get this working by using

{"$set": {x:profile.x, y:profile.y}}

in the update, does anyone have a generic solutions so that I can pass in any profile:

profile = { .... }
Share Improve this question asked Feb 23, 2014 at 23:35 avronoavrono 1,6803 gold badges20 silver badges41 bronze badges 5
  • Does the "profile" document contains the _id? – Yooz Commented Feb 23, 2014 at 23:43
  • I am updating based on say username ... {username:username}, {$set:{ ... } --- and "profile" does not have the _id, but it could ... – avrono Commented Feb 23, 2014 at 23:47
  • 1 Yes I get that, I was asking because if you have the _id in your profile document, you could use the collection().save which updates/replaces a full document – Yooz Commented Feb 23, 2014 at 23:49
  • @Yoann you were 100% right that does the trick. Thanks ! – avrono Commented Feb 24, 2014 at 0:05
  • Thanks! I just added the answer, if you want to valid ;) – Yooz Commented Feb 24, 2014 at 0:06
Add a ment  | 

3 Answers 3

Reset to default 2

If "profile" document contains the _id, you can use the collection().save which updates/replaces a full document.

http://mongodb.github.io/node-mongodb-native/api-generated/collection.html

There's nothing wrong with using save(), but update() will work also if the document already exists, and the update document does not need to contain the _id as it will be preserved by the update(). The primary advantage of save() is that it will insert the document if it doesn't already exist (called "upsert"). For example, this mongo shell script:

db.collection.insert({_id:0, x:1, y:2})
printjson(db.collection.findOne())

db.collection.update({_id:0}, {x:3, y:4, z:5})
printjson(db.collection.findOne())

db.collection.save({_id:0, x:6, y:7, z:8, w:9})
printjson(db.collection.findOne())

produces this output, showing that update() also updates the full document selected by id:

{ "_id" : 0, "x" : 1, "y" : 2 }
{ "_id" : 0, "x" : 3, "y" : 4, "z" : 5 }
{ "_id" : 0, "x" : 6, "y" : 7, "z" : 8, "w" : 9 }

If you use a platform like Meteor you will not have the option to use save() as the platform does not "yet" support the full range of MongoDB mands. As Bruce stated, I've had success passing in a JSON Object with update() and had no issues updating a full document. I've done the following with Meteor:

var myUpdatedData = {item1:"data1",item2:"data2",item3:"data3"};
MyCollection.update({_id: existingID}, myUpdatedData));
发布评论

评论列表(0)

  1. 暂无评论