12

I am trying to update an array in a mongoDB from a node.js program. I am able to modify the array from within node.js, but I can not get the changes to save.

http://pastebin.com/j0Mnf7jP

I think I am doing something very wrong. assistance would be appreciated...

1
  • You can do this from mongo shell alone. Commented Sep 2, 2013 at 9:58

4 Answers 4

11

Change this line:

({_id:doc._id},$set:{scores:zz});

To:

({_id:doc._id}, { $set:{scores:zz}} );

This should also probably be wrapped with a callback, to catch errors:

db.schools.update({_id:doc._id}, {$set:{scores:zz}}, function(err, result) {
    if (err)
        //do something.
});
Sign up to request clarification or add additional context in comments.

5 Comments

I did this... But I am getting this error... TypeError: Cannot call method 'update' of undefined
@IrishGringo -- sounds like your db or schools is undefined.
use db.collection( 'students' ) instead of db.schools
The callback is essential! Had other api routes in my app(write/reads), and copied the format of one, before trying to perform an update action. I took out the callback to maintain function simplicity. Have spent the past hour trying to get an update to occur, when all worked fine within the shell.
what is result in this case?
8

I know it's a bit late to help you now, but maybe others can benefit as new cohorts pass through MongoDB University!

db.schools.update should read db.students.update.

@tymeJV's answer gives the rest:

  • Wrap the $set inside braces: {$set:{scores:zz}}
  • Add a callback function to catch errors:

    db.collection( 'students' ).update (
        { _id : doc._id },
        { $set : { scores:zz } },
        function( err, result ) {
            if ( err ) throw err;
        }
    );
    

Funnily enough, I'm actually doing exactly the same assignment right now! I had a different issue that was answered by reading the docs, but I saw this question while googling for it. Hope I helped someone!

Comments

1
var dbName = 'school'
var tableName = 'classA'
MongoClient.connect(dbName, function(err, db) {
  if (err)
  {
    console.log(err);
  }
  else {

    var collection = db.collection(tableName)
    collection.update({_id:doc._id}, {$set:{scores:zz}}, function(err, result) {
      if (err)
      {
        console.log(err);

      }
      else{
        console.log(result);
      }
    });
  }
});

Comments

0

I think you should do following code for solving issues

    var lowScore = 9999.9;
        for ( var i=0; i<doc.scores.length; i++ ) {
            if ( doc.scores[i].type == "homework"
               && doc.scores[i].score < lowScore ) {
                    lowScore = doc.scores[i].score;
            }
        }

and then update your collection using following query

collection.update({ "_id":doc._id }, 
{ $pull : { "scores" : {
$and: [ {"type":"homework"}, { "score":lowScore} ]
} } },
{ "safe":true },
function( err, result ) {
if (err) {
 console.log(err);
  }
} // update callback
); 

for more info you can refer here

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.