vendredi 20 février 2015

How to add a property (property name is variable) to object via MongoDB driver (node.js)

The structure of my document in collection is this.



{
"_id" : ObjectId("54e74de2950fd3a4e5f0a37a"),
"userName": "Any_user",
"ratings": {
"rating1" : [ ],
"big_rating" : [ ]
}
}


In MongoDB client (mongo.exe) the adding i need looks like this



db.userContent.update({userName: "NXTaar"}, {$set: {"ratings.variable_name": []}})


I need the function for the MongoDB node.js driver And yes, i know the "property name as string" thing there i can pass the variable to the property name. Here is what i tried to make:



var rating = {};
var title = 'some_string';
ratings[title] = [];

usercontent.update({userName: "some_user"}, {$set: ratings}, function (err, doc) {...})


this line adds a new object to the document with a name of property



usercontent.update({userName: "some_user"}, {$set: {ratings: ratings}}, function (err, doc) {...})


and this one does exactly what i need, but each time it runs, it rewrites current property with a new one(yes, i know, it because of $set parameter which changes selected object with the thing that you've passed to it)


I need a function which adds a property to object with a name given through variable


Aucun commentaire:

Enregistrer un commentaire