Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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?

share|improve this question
    
What's the object that's logged on console.log(req.body)? – chridam May 12 '15 at 7:43
    
for example: { name: 'Test', number: '2', row: '2' } – mk2015 May 12 '15 at 8:13

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);
    });
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.