Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Meteor receive updates to the results of a MongoDB query?

I asked a question a few months ago, to which Meteor seems to have the answer.

Which, if any, of the NoSQL databases can provide stream of *changes* to a query result set?

How does Meteor receive updates to the results of a MongoDB query?

Thanks,

Chris.

like image 795
fadedbee Avatar asked Apr 11 '12 09:04

fadedbee


People also ask

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.

What is Meteor MongoDB?

Meteor provides a complete open source platform for building web and mobile apps in pure JavaScript. The Meteor team chose MongoDB as its datastore for its performance, scalability, and rich features for JSON. Meteor apps run using JavaScript via Node. JS on the server and JavaScript in the phone's browser.

How does MongoDB query work?

MongoDB server itself does not directly read or write the data to the files or disk or memory. After passing the received queries to the storage engine, the storage engine is responsible to read or write the data in the files or memory basically it manages the data.

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.


2 Answers

You want query.observe() for this. Say you have a Posts collection with a tags field, and you want to get notified when a post with the important tag is added.

http://docs.meteor.com/#observe

// collection of posts that includes array of tags
var Posts = new Meteor.Collection('posts');

// DB cursor to find all posts with 'important' in the tags array.
var cursor = Posts.find({tags: 'important'});

// watch the cursor for changes
var handle = cursor.observe({
  added: function (post) { ... }, // run when post is added
  changed: function (post) { ... } // run when post is changed
  removed: function (post) { ... } // run when post is removed
});

You can run this code on the client, if you want to do something in each browser when a post changes. Or you can run this on the server, if you want to say send an email to the team when an important post is added.

Note that added and removed refer to the query, not the document. If you have an existing post document and run

Posts.update(my_post_id, {$addToSet: {tags: 'important'}});

this will trigger the 'added' callback, since the post is getting added to the query result.

like image 97
debergalis Avatar answered Sep 19 '22 23:09

debergalis


Currently, Meteor really works well with one instance/process. In such case all queries are going through this instance and it can broadcast it back to other clients. Additional, it polls MongoDB every 10s for changes to the database which were done by outside queries. They are plans for 1.0 to improve the scalability and hopefully allow multiple instances to inform each one about changes.

DerbyJS on the other hand is using Redis PubSub.

like image 21
Mitar Avatar answered Sep 21 '22 23:09

Mitar