Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.Collection and Meteor.Collection.Cursor

Tags:

mongodb

meteor

What is

Meteor.Collection 

and

Meteor.Collection.Cursor

?

How does these two related to each other? Did:

new Meteor.Collection("name") 

create a MONGODB collection with the parameter name?

like image 520
sha mohd Avatar asked Feb 22 '14 10:02

sha mohd


People also ask

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.

Which of the following function will remove collection in MongoDB?

MongoDB's remove() method is used to remove a document from the collection.

How do I search in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.


2 Answers

Did new Meteor.Collection("name") create a MONGODB collection with the parameter name?

Not exactly. A Meteor.Collection represents a MongoDB collection that may or may not exist yet, but the actual MongoDB collection isn't actually created until you insert a document.

A Meteor.Collection.Cursor is a reactive data source that represents a changing subset of documents that exist within a MongoDB collection. This subset of documents is specified by the selector and options arguments you pass to the Meteor.Collection.find(selector, options) method. This find() method returns the cursor object. I think the Meteor Docs explain cursors well:

find returns a cursor. It does not immediately access the database or return documents. Cursors provide fetch to return all matching documents, map and forEach to iterate over all matching documents, and observe and observeChanges to register callbacks when the set of matching documents changes.

Collection cursors are not query snapshots. If the database changes between calling Collection.find and fetching the results of the cursor, or while fetching results from the cursor, those changes may or may not appear in the result set.

Cursors are a reactive data source. The first time you retrieve a cursor's documents with fetch, map, or forEach inside a reactive computation (eg, a template or autorun), Meteor will register a dependency on the underlying data. Any change to the collection that changes the documents in a cursor will trigger a recomputation. To disable this behavior, pass {reactive: false} as an option to find.

The reactivity of cursors is important. If I have a cursor object, I can retrieve the current set of documents it represents by calling fetch() on it. If the data changes in between calls, the fetch() method will actually return a different array of documents. Many things in Meteor natively understand the reactivity of cursors. This is why we can return a cursor object from a template helper function:

Template.foo.documents = function() {
  return MyCollection.find(); // returns a cursor object, rather than an array of documents
};

Behind the scenes, Meteor's templating system knows to call fetch() on this cursor object. When the server sends the client updates telling it that the collection has changed, the cursor is informed of this change, which causes the template helper to be recomputed, which causes the template to be rerendered.

like image 181
sbking Avatar answered Sep 30 '22 13:09

sbking


A Meteor.Collection is an object that you would define like this:

var collection = new Meteor.Collection("collection");

This object then lets you store data in your mongo database. Note just defining a collection this way does not create a collection in your mongo database. The colleciton would be created after you insert a document in.

So in this way you would not have a collection called name until you insert a document to it.

A cursor is the result of a .find() operation:

var cursor = collection.find()

You may have 1000s of documents, the cursor lets you go through them, one by one, without having to load all of them into your server's RAM.

You can then loop through using forEach, or use some of the other operations as specified in the docs : http://docs.meteor.com/#meteor_collection_cursor

A Cursor is also a reactive data source on the client, so if data changes, you can use the same query to update your DOM.

As Neil mentions its also worthwhile knowing Mongo is a NoSQL database. This means you don't have to really create tables/collections. You would just define a collecction like above, then insert a document to it. This way the collection would be created if it didn't exist. If it already existed, it would be inserted into that collection instead.

Browsing your local database

You don't really need to concern yourself with MongoDB until you are publishing your app, you can just interact with it using Meteor alone. In case you want to have a look at what it looks like:

If you want to have a look at your Mongo database. While meteor is running, in the same directory use meteor mongo to bring up a mongo shell, or use a tool like robomongo (Gui tool) to connect to localhost on port 3002 to have a peek at what your mongo database looks like.

like image 44
Tarang Avatar answered Sep 30 '22 13:09

Tarang