Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update a Mongo.db collection in meteor.js?

I have a collection that I need to update when the user presses a button. I just need to change one variable to another.

In the console, this line of code works:

  db.users.update({username : "Jack"},{age : 13, username : "Jack"});

But when I put in this code:

Template.body.events({
'click #updateAge' = function() {
         {
              alert();
              db.users.update({username : "Jack"},{age : 13, username : "Jack"});
         }
    }

})

into my JavaScript file for Meteor.js, it doesn't do anything at all (I don't get an error message, and I see the alert, but the update just doesn't work).

I've read through the Meteor Documentation on updating, but I just can't seem to get it to work. Does anybody know what I'm doing wrong here?

like image 289
Ian Wise Avatar asked Aug 01 '14 23:08

Ian Wise


1 Answers

Found the problem.

Since I defined my database in my lib.js file

users = new Meteor.collection("users");

I don't need to put a db in front of the db.users.update({_id : "Jack"},{...}). I also need to find the document using the given mongo _id, not the identifier "username".

so the appropriate code would be

users.update({_id : "Jack"},{$set:{age : 13, username : "Jack"}});
like image 62
Ian Wise Avatar answered Oct 14 '22 19:10

Ian Wise