0

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 the http post in angular controller:

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

I want to update in the Document Serverraum1 tables. How can i push $scope.table in tables?

2
  • What's the object that's logged on console.log(req.body)? Commented May 12, 2015 at 7:43
  • for example: { name: 'Test', number: '2', row: '2' } Commented May 12, 2015 at 8:13

1 Answer 1

0

To update in the document serverraum1 tables by pushing $scope.table, the mongodb update function your POST method should use the $addToSet operator which adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array:

app.post('/serverraeume', function (req, res) {
    console.log(req.body);
    var query = {"name" : "Serverraum1"}, // the query object to find records that need to be updated
        update = { "$addToSet": { "tables": req.body } }, // the replacement object
        options = {}; // defines the behaviour of the function
    db.serverraeume.update(query, update, options, function (req, res) {
        console.log(res);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.