Meteor Cheatsheet Collections

meteor-cheat-sheet

// Meteor - Collections:

Entries.remove({}); 
DeckSubscriptionList.remove({}); 
DeckList.remove({}); 
Meteor.users.remove({}); 
UserPreferences.remove({}); 
Tags.remove({}); 
StudyResults.remove({});

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. They also update themselves 
automatically, so a view component backed by a collection will automatically display the most 
up-to-date data.

Creating a new collection is as easy as calling MyCollection = new Mongo.Collection("my-collection"); 
in your JavaScript. On the server, this sets up a MongoDB collection called my-collection; on the 
client, this creates a cache connected to the server collection.

For now we can write our code with the assumption that the entire database is present on the client.
To create the collection, we define a new tasks module that creates a Mongo collection and exports it:

imports/collections/tasks.js:
import { Mongo } from 'meteor/mongo';
export const Tasks = new Mongo.Collection('tasks');

We need to import this module on the server (which creates the MongoDB collection and sets up the 
plumbing to get the data to the client):

server/main.js:
import '../imports/collections/tasks.js';

We update our client-side JavaScript code to get our tasks from a collection instead of a static array:

imports/ui/body.js:
import { Template } from 'meteor/templating';
import { Tasks } from '../api/tasks.js';
import './body.html';
Template.body.helpers({
  tasks() {
    return Tasks.find({});
  },
});

When you make these changes to the code, you'll notice that the tasks that used to be 
in the todo list have disappeared. That's because our database is currently empty — we 
need to insert some tasks!  Items inside collections are called documents. Let's use the 
server database console to insert some documents into our collection. In a new terminal 
tab, go to your app directory and type:

meteor mongo

This opens a console into your app's local development database. Into the prompt, type:

db.tasks.insert({ text: "Hello world!", createdAt: new Date() });

Products = new Mongo.Collection('products');
Products.insert({img: '/bread.png', name: 'Bread', place: 'fridge'});
Products.find({_id: 'xxx'});
Posts.find({}, {fields: {timestamp: 0}}); // Exclude the timestamp field from being published.
player.find( { Winner: { $lt: 1 }, sessionIDz: "FmwgXvxHZmuAwSzpe" }).fetch()
userLive.find({readedAt: {$gte: startDay, $lt: endDay}});

var queryDocument = { _id: 'xxx' };
var changeDocument = { $set: { place: 'fridge' } };
Products.update(queryDocument, changeDocument);

Products.find({"plant.color": {$exists: 1}});
Products.find({"plant.color": {$in: ["White"]}});
Posts.find({}, {sort: {timestamp: -1}, limit: {30}});
Products.find({_id: {$in: idArray}}, {limit: 10, skip: 100});

CollectionName.update(selector, changes, options, callback);
CollectionName.update(id, {$set: {"plants.0.color": "Blue"}});
UserPreferences.update({userId: this.userId},{$set: {currentDeckId: deckId }});

CollectionName.find({}, {sort: {position: 1}, limit: 10, fields: {name: 1, body: 0} });
CollectionName.find({name: {$in: usedTags}});
CollectionName.find(
  {tags: {$in: search}},
  {sort: {position: 1}}
)

queryDoc = {title: {$regex: new RegExp(searchTerm)}};
return DeckList.find(queryDoc).fetch();

var attributes = {
  createdBy: Meteor.userId(),
  content: "Today was a good day."
}
CollectionName.insert(attributes, function(error, response){
});
Modules.update({_id: _id}, {$set: attributes}, function(error, response){
})
CollectionName.update(
   { sku: "abc123" },
   { $inc: { quantity: -2, "metrics.orders": 1 } }
)

// To create a non-reactive cursor:
CollectionName.find({fridgeId: user},{reactive: false});
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License