Intoduction
In this article you are going to find out about working with MongoDB in MeteorJS. We will consider main functionality and MongoDB DBSM characteristics, touch upon some subtle questions of integration and use of Mongo together with Meteor platform. The article is useful both for beginners and for experienced MeteorJS developers.
All the examples below are available on our Github page. At the time of writing an article version 1.4 of MeteorJS is a current one with Mongo 3.2 support.
About MongoDB
MongoDB is an open cross-platform document-oriented DBMS. The MeteorJS platform itself is tightly integrated with MongoDB.
MeteorJS developers made several innovative changes in integration process of MeteorJS with MongoDB. One of such solutions is MiniMongo. MiniMongo partially supports Mongo API and serves as a temporary data store at the client-side. Thus, developers do not take part in providing a client-server communication leaving this work to Meteor platform.

NoSQL Mongo DBSM has been the only and the main data storage for Meteor for a long time. And it is no surprise, as Mongo offers a really wide range of opportunities:
Scalability. MongoDB is highly scalable. Standalone server is used by default, but it can be easily expanded to a cluster.
Document-orientation. Document is the main storage unit in MongoDB. A document may include other documents or their massives. It is a very flexible feature and it allows to accelerate development process dramatically.
Fast-acting. MongoDB has good efficiency indicators. According to Cloud Serving Benchmark by Yahoo! MongoDB is able to perform about 193 000 operations per second, whereas Cassandra performs about 110 000 operations and Couchbase even less - 31 000.
Integration with popular web-services. MongoDB is rather popular and widely used. Google Cloud Platform, Amazon Web Services, Microsoft Azure and many other web-services have a built-in support of MongoDB.
Efficient work with big files. MongoDB Stores files in BJSON format and limits document’s size to 16MB. It is recommended to use GridFS to work with documents of a larger size.
Availability and free of charge use. MongoDB is distributed under licenses GNU AGPL and Apache License 2.0. It allows using Mongo DBMS in the majority of projects. MongoDB source code is available at Github, which makes it much easier to understand the work of Mongo’s internal mechanisms.
Logging and profiling. MongoDB has built-in tools for logging and profiling. It helps to optimize the operation with DB.
Even having all of its advantages MongoDB is not a perfect solution and cannot entirely substitute other DBMSs. That is why Meteor Development Group decided to add SQL-DBMS support. Meteor developers community has already come up with several solutions, but official support is only being planned.
MongoDB & MeteorJS
Data is grouped by collections in MongoDB. Collection, as a rule, consists of documents of the same type, but this is an optional condition. Documents of the collection are not limited by scheme and can differ from each other.

It is very simple to create Mongo collection in MeteorJS. Let’s consider the example.
First of all you need to create a new MeteorJS application: meteor create collections-test
.
As a result of this command client
and server
directories will also be added as an example of Meteor application code. We do not need these files. Delete them.
cd collections-test
rm -rf client server
We need to announce a new Mongo collecton. Let’s make it in collections.js
file.
import { Mongo } from 'meteor/mongo';
const pl = new Mongo.Collection('programmingLanguages');
export { pl as ProgrammingLanguages };
We have just created an empty Mongo collection programmingLanguages
.
Now we can conduct CRUD (create, read, update, delete) operations with this collection using the inbuilt MeteorJS methods: insert
, find
and findOne
, update
and upsert
, remove
. Let’s consider the given functionality in the examples. Methods find
and findOne
allow to get documents from the collection. Method find
is used to sample from a variety of documents and returns the cursor, but findOne
returns the first found document to the collection or undefined
if the document was not found. You can transmit search parameters, also called "query filters" to methods.
// find all programming languages in collection
ProgrammingLanguages.find();
// find a programming language JavaScript
ProgrammingLanguages.findOne({name: 'JavaScript'});
// or
ProgrammingLanguages.find({name: 'JavaScript'}).fetch()[0];
// find the programming languages the name of which includes`Java`
ProgrammingLanguages.find({name: /.*Java.*/});
// amount of programming languages the name of which includes `Java`
ProgrammingLanguages.find({name: /.*Java.*/}).count();
You can add new elements to a collection with the help of insert
method.
ProgrammingLanguages.insert({name: 'JavaScript'});
In the next step we will try to fill the collection with data. After the completion of Meteor initialization we will have to find out the amount of elements in programmingLanguages
collection and add necessary documents, if the collection is empty.
We implement the described above functionality in fill-db.js
file. Let’s place this file in server
directory, for script to be run only on the server side.
import { Meteor } from 'meteor/meteor';
import { ProgrammingLanguages } from '/collections';
Meteor.startup(() => {
const count = ProgrammingLanguages.find().count();
if (count === 0) {
ProgrammingLanguages.insert({
name: 'JavaScript',
stars: 0
});
ProgrammingLanguages.insert({
name: 'Python',
stars: 0
});
ProgrammingLanguages.insert({
name: 'Ruby',
stars: 0
});
}
});
Start the application by meteor
command from collections-test
directory.
When you first launch the app ProgrammingLanguages
collection is empty. Accordingly, the call ProgrammingLanguages.find().count()
wiil return the value of 0
. That is why we will add three documents with name
and stars
fields to the collection.
MeteorJS platform also provides an interactive shell to work with Mongo. Let’s check the resuts of fiil-db.js
script work with the help of MongoDB shell.
Start MongoDB shell from the directory of MeteorJS application (without stopping the work of MeteorJS server) and request for all the documents from programmingLanguages
collection:
meteor mongo
db.programmingLanguages.find()

As you can see in the screenshot, when you add documents to the collection, _id
field is created for each of them. This field contains ObjectID - a unique identifier of a document.
Now consider the concrete example of front-end part with MongoDB. Now we implement a separate page on the basis of Blaze template, where users can vote for the programming language by changing the value of stars
field. The code must work only on the client-side and so all the files will have to be placed in client
directory.
In app.html
file we will display the list of programmingLanguages
via programmingLanguages helper.
ProgrammingLanguages
helper must receive the list of programmingLanguages
from MongoDB and return them to Blaze template. Each programming language is displayed with the help of programmingLanguage template. The template allows to display the following set of data: the name of the programing language, the amount of stars and .pl-star
link, by pressing which a user can increase the language rating.
In app.js
file we will describe programmigLanguages
helper and process the click event on the .pl-star
link.
<body>
{{# each programmingLanguages}}
{{> programmingLanguage}}
{{/ each}}
</body>
<template name="programmingLanguage">
<p>
<b>{{name}} {{stars}}</b>
<a href="#" class="pl-star">☆</a>
</p>
</template>

User can change the value of stars
field of each document from ProgrammingLanguages
collection. On account of fullstack reactivity each client of the application will see current condition of collection even without page reloading.
Meteor MongoDB packages
Meteor developers’ community has created a lot of useful packages, which make the work with Mongo DBMS easier and more efficient. AstronomyCollection2 should be specially pointed out. These packages allow to use schemes for MongoDB with the clear assignment of stored data types, add validation support, expand the collections’ behaviour and even generate UI based on the collection’s scheme.
Conclusion
MeteorJS platform is closely integrated with MongoDB. Lots of solutions were created to improve and simplify the work with Mongo DBMS in Meteor. For now, MongoDB remains the main DBMS for Meteor, but there is an active work on the introduction of other databases, including SQL.