Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor/Mongo: Finding and updating certain elements in a collection

I'm starting off with Meteor and need some help with Mongo. I have a collection of names that I'm displaying on a list and want to be able to update one variable of certain entries in the database based on other criteria. Basically what I want to do is:

For every entry where characteristic A = true and B = true, change characteristic C to be false.

So far, I've been trying to figure out how Mongo can handle a "for each" loop over the elements of the collection, and for each element check if conditions A and B hold, and then collection.update(element, {C: false}). This is proving to be a lot more problematic than I thought. I want to do something like this (using dummy variable names):

for (i = 0; i < collection.find().count(); i++){
    if (collection[i].A===true && collection[i].B===true)
        collection.update(collection[i], {$set: {C: false}});
};

I've been changing this base code around, but am starting to sense that I'm missing something basic about indexing/how collections work in Mongo. Can you index a collection like this (and if so, is this even the most convenient way to do what I'm trying to do?)?

like image 953
ajyang818 Avatar asked Dec 24 '12 01:12

ajyang818


People also ask

What is Meteor in MongoDB?

Meteor stores data in collections. To get started, declare a collection with new Mongo.

What is Meteor collection?

Collections are Meteor's way of storing persistent data. The special thing about collections in Meteor is that they can be accessed from both the server and the client, making it easy to write view logic without having to write a lot of server code.

What is Minimongo?

minimongo is a lightweight, schemaless, Pythonic Object-Oriented interface to MongoDB. It provides a very thin, dynamicly typed (schema-less) object management layer for any data stored in any MongoDB collection. minimongo directly calls the existing pymongo query syntax.

How do I link my meteor to MongoDB?

Open a terminal window and run meteor command. It will start running on localhost:3000 if you have not changed to port. Go to Robomongo (or your favorite mongodb client software) and create a new connection, making sure to change the connection address to localhost and the given the port number.


2 Answers

Of course I figure out how to do this right after posting, and of course it's suggested in the Meteor documentation!

And, of course, it's a simple solution:

collection.update({A: true, B: true}, {$set: {C:false}});
like image 74
ajyang818 Avatar answered Oct 23 '22 12:10

ajyang818


As already suggested in comments, the correct answer is:

collection.update({A: true, B: true}, {$set: {C:false}}, {multi: true});

(At least in pure MongoDB, see there).

Without multi: true it will change only one document matching the criteria.

In Meteor it is a bit more tricky, as you are not allowed to do client-side updates other than by matching it (so no possibility for various criteria, no possibility for multi), see http://docs.meteor.com/#update.

You can iterate over all finds, but it would be better to run such code server-side.

like image 9
Piotr Migdal Avatar answered Oct 23 '22 12:10

Piotr Migdal