Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  74] [ 2]  / answers: 1 / hits: 15954  / 10 Years ago, sun, february 23, 2014, 12:00:00

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 = { .... }

More From » node.js

 Answers
6

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


[#72355] Friday, February 21, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
;