This will setup a mongo collection on the server -- has the full MongoDB API on the server so you can perform any mongo operation on it.This will setup a minimongo collection on the client – “essentially an in-memory, non-persistent implementation of Mongo in pure JavaScript. It serves as a local cache that stores just the subset of the database that this client is working with. Queries on the client (find) are served directly out of this cache, without talking to the server.”----Can get fancy with collections:- if you need close to real time operations you can pass in null as the connection and it will be a memory basedcollectionpass in a transform function to run on every document -- useful if you want to convert the JSON document to your own typeFor this presentation we will stick to mongo----The client collection will be empty until we setup a publish on the server,and subscribe to it on the client.
You can have multiple publish and subscriptions to the same collection. In your view, just make sure to filter the local collection to pull out what you want.You can also reactively subscribe to collections.---The autopublish package is useful for development because it publishes and subscribes to every collection automatically – so don’t forget to remove that before deploying.
Query collections with the mongo operators
View all the operators here:http://docs.mongodb.org/manual/reference/operator/
There are a few limitations on minimongo (operations on client collection).Overcome the limitations by performing these behind server methods, the server collections have access to the full mongo api.See detailed limitations here https://github.com/meteor/meteor/blob/devel/packages/minimongo/NOTES
find() returns a cursor and unless you specify otherwise, it will be reactive.---forEach, map, fetch – each can only be called once until you call rewind()Observe and observeChanges
Observe allows you to watch for when a query changes.observeChanges is more performant than observe, but only sends the fields that changed
The insert function on a collection takes a document.--Update takes a selector (same as find) or an id, and a mongo modifier.If a modifier doesn't contain any $-operators, then it is instead interpreted as a literal document, and completely replaces whatever was previously in the databaseIf you are on the server, you can specify that it should update all matching documents (it defaults to only updating one document).--Remove takes a selector.--Insert, update and remove all have an optional last parameter for a callback that passes an error (if there was one).
Validate insert, update, and remove functions to prevent malicious client updates and to enforce validation.Meteor has a check package which makes input validation much easier: http://docs.meteor.com/#matchThere is also a deny function, which is the opposite of the allow function.