In this post, I show how to manipulate data in Meteor and MongoDB for apps under development.

The example used in this post in on github at simple-todos.

Contents

  1. Setup

  2. Mongo Shell

  3. Mongo Commands

  4. Mongo Commands Summary

  5. Deleting all MongoDB data

1. Setup

In a terminal, I start my simple-todos app with the meteor command:

git clone https://github.com/RemyNoulin/simple-todos.git
cd simple-todos
meteor

2. Mongo Shell

I start a new terminal, go the app folder and enter the command meteor mongo. meteor mongo starts mongo shell for the app.

cd simple-todos
meteor mongo

3. Mongo Commands

This section shows the mongo commands and the results.

Starting with show collections:

meteor:PRIMARY> show collections
system.indexes
tasks
users

The users collection is created by meteor account system and has the id data for each user.

Running db.tasks.find() lists all the documents in a collection.

meteor:PRIMARY> db.tasks.find()
{ "_id" : "cTRus8BWYNmn93hsZ", "createdAt" : ISODate("2016-02-14T10:01:26.112Z"), "owner" : "exjGMZjJTn6mMcioA", "private" : false, "text" : "qweqwe", "username" : "qwe" }

insert command:

meteor:PRIMARY> db.tasks.insert({"_id":"56c057f4277bf6d36569114f","createdAt" : ISODate("2016-02-14T10:01:26.112Z"), "owner" : "exjGMZjJTn6mMcioA", "private" : false, "text" : "MONGO SHELL", "username" : "qwe"})
meteor:PRIMARY> db.tasks.find()
{ "_id" : "cTRus8BWYNmn93hsZ", "createdAt" : ISODate("2016-02-14T10:01:26.112Z"), "owner" : "exjGMZjJTn6mMcioA", "private" : false, "text" : "qweqwe", "username" : "qwe" }
{ "_id" : "56c057f4277bf6d36569114f", "createdAt" : ISODate("2016-02-14T10:01:26.112Z"), "owner" : "exjGMZjJTn6mMcioA", "private" : false, "text" : "MONGO SHELL", "username" : "qwe" }

Nested json query:

meteor:PRIMARY> db.tasks.insert({"_id":"4rKB2437z2ouvg5J7","createdAt" : ISODate("2016-02-14T10:01:26.112Z"), "owner" : "exjGMZjJTn6mMcioA", "private" : false, "text" : "MONGO SHELL", "username" : "qwe", "doc": {"deadline": "no"}})
meteor:PRIMARY> db.tasks.findOne({ "_id" : "4rKB2437z2ouvg5J7"})
{
        "_id" : "4rKB2437z2ouvg5J7",
        "createdAt" : ISODate("2016-02-14T10:01:26.112Z"),
        "owner" : "exjGMZjJTn6mMcioA",
        "private" : false,
        "text" : "MONGO SHELL",
        "username" : "qwe",
        "doc" : {
                "deadline" : "no"
        }
}
meteor:PRIMARY> db.tasks.find({"doc.deadline": "no"})
{ "_id" : "4rKB2437z2ouvg5J7", "createdAt" : ISODate("2016-02-14T10:01:26.112Z"), "owner" : "exjGMZjJTn6mMcioA", "private" : false, "text" : "MONGO SHELL", "username" : "qwe", "doc" : { "deadline" : "no" } }

Fields in documents are modified with the update command and the $set operator:

meteor:PRIMARY> db.tasks.update({ "_id" : "56c057f4277bf6d36569114f"}, {$set: {"private": true}})
meteor:PRIMARY> db.tasks.findOne({"_id": "56c057f4277bf6d36569114f"})
{
        "_id" : "56c057f4277bf6d36569114f",
        "createdAt" : ISODate("2016-02-14T10:01:26.112Z"),
        "owner" : "exjGMZjJTn6mMcioA",
        "private" : true,
        "text" : "MONGO SHELL",
        "username" : "qwe"
}

Remove command:

meteor:PRIMARY> db.tasks.remove({"_id" : "56c057f4277bf6d36569114f"})

count command counts the documents:

meteor:PRIMARY> db.users.count()
1

drop command empties a collection:

meteor:PRIMARY> db.tasks.drop()
true

4. Mongo Commands Summary

show collections:                              List collections
db.<collection>.find():                        Queries
db.<collection>.insert():                      Insert document
db.<collection>.update(,):                     Update document
db.<collection>.remove({“_id” : “ID_STRING”}): Remove a document
db.collection.count():                         Returns total number of documents in the collection
db.collection.find( <query> ).count():         Returns the total number of documents that match the query
db.<collection>.drop():                        Delete a collection
db.<collection>.help():                        List collection commands

5. Deleting all MongoDB data

cd simple-todos
meteor reset