javascript - How to update Document in MongoDB via Node.js and Angular.js -


my document in mongodb looks like:

{     "_id" : objectid("5550e710dad47584cbfe9da1"),     "name" : "serverraum1",     "tables" : [                   {                     "name" : "table1",                     "nummer" : "1",                     "reihe" : "2",                    }                ] } 

post method:

app.post('/serverraeume', function (req, res) {     console.log(req.body);     db.serverraeume.update(req.body, function (req, res) {     }); }); 

and http post in angular controller:

$scope.addschrank = function (serverraum){       $http.post('/serverraeume', $scope.table); } 

i want update in document serverraum1 tables. how can push $scope.table in tables?

to update in document serverraum1 tables pushing $scope.table, mongodb update function post method should use $addtoset operator adds value array unless value present, in case $addtoset nothing array:

app.post('/serverraeume', function (req, res) {     console.log(req.body);     var query = {"name" : "serverraum1"}, // query object find records need updated         update = { "$addtoset": { "tables": req.body } }, // replacement object         options = {}; // defines behaviour of function     db.serverraeume.update(query, update, options, function (req, res) {         console.log(res);     }); }); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -