MongoDB installation Ubuntu

If you are using Unbuntu, the following link is an excellent reference:

MongoDB installation procedures for MacOS-X

With homebrew installed, run $ brew install mongodb. Install brew services with $ brew tap homebrew/services. Homebrew services allow you to easily add services like MongoDB to startup.
Run the command brew services start mongodb
Visit http://localhost:27017 to verify installation. If MongoDB has been installed correctly you will see the message It looks like you are trying to access MongoDB over HTTP on the native driver port. at the top of the browser window.
For Linux, consult the install guide at If you are using Windows, please see the Windows install guide at:

MongoDB dir macosx The databases are stored in the /usr/local/var/mongodb/ directory The mongod.conf file is here: /usr/local/etc/mongod.conf The mongo logs can be found at /usr/local/var/log/mongodb/ The mongo binaries are here: /usr/local/Cellar/mongodb/[version]/bin

To start mongodb server

$ sudo mongod --config /usr/local/etc/mongod.conf

To check current installed version

$ mongo -version

Check to see if any mongo service is running

$ launchctl list | grep mongo

If you have installed mongodb through homebrew then you can simply start mongodb through

$ brew services start mongodb

Then access the shell by

$ mongo

You can shut down your db by

$ brew services stop mongodb

For more options

$ brew info mongodb

MongoDB shell commands

Basic commands

To do this Run this command Example
Connect to local host on default port 27017 mongo mongo
Connect to remote host on specified port mongo –host --port mongo –host –port 23020
Connect to a database mongo / mongo
Show current database db db
Select or switch database use use mydb
Execute a JavaScript file load() load (myscript.js)
Display help help help
Display help on DB methods
Display help on Collection

Show commands

To do this Run this command Example
Show all databases show dbs show dbs
Show all collections in current database show collections show collections
Show all users on current database show users show users
Show all roles on current database show roles show roles

CRUD Operations

To do this Run this command Example
Insert a new document in a collection db.collection.insert( ) db.books.insert({“isbn”: 9780060859749, “title”: “After Alice: A Novel”, “author”: “Gregory Maguire”, “category”: “Fiction”, “year”:2016})
Insert multiple documents into a collection db.collection.insertMany([ , , ... ]) db.books.insertMany( [{“isbn”: 9780198321668, “title”: “Romeo and Juliet”, “author”: “William Shakespeare”, “category”: “Tragedy”, “year”: 2008}, {“isbn”: 9781505297409, “title”: “Treasure Island”, “author”: “Robert Louis Stevenson”, “category”: “Fiction”, “year”:2014}])
Show all documents in the collection db.collection.find() db.books.find()
Filter documents by field value condition db.collection.find() db.books.find({“title”:”Treasure Island”})
Show only some fields of matching documents db.collection.find(, ) db.books.find({“title”:”Treasure Island”}, {title:true, category:true, _id:false})
Show the first document that matches the query condition db.collection.findOne(, ) db.books.findOne({}, {_id:false})  
Update specific fields of a single document that match the query condition db.collection.update(, ) db.books.update({title : “Treasure Island”}, {$set : {category :”Adventure Fiction”}})
Remove certain fields of a single document the query condition db.collection.update(, ) db.books.update({title : “Treasure Island”}, {$unset : {category:””}})
Remove certain fields of all documents that match the query condition db.collection.update(, , {multi:true} ) db.books.update({category : “Fiction”}, {$unset : {category:””}}, {multi:true})
Delete a single document that match the query condition db.collection.remove(, {justOne:true}) db.books.remove({title :”Treasure Island”}, {justOne:true})
Delete all documents matching a query condition db.collection.remove() db.books.remove({“category” :”Fiction”})
Delete all documents in a collection db.collection.remove({}) db.books.remove({})


To do this Run this command Example
Create an index db.collection.createIndex({indexField:type}) Type 1 means ascending; -1 means descending db.books.createIndex({title:1})
Create a unique index db.collection.createIndex({indexField:type}, {unique:true} ) db.books.createIndex({isbn:1},{unique:true})
Create a index on multiple fields (compound index) db.collection.createIndex({indexField1:type1, indexField2:type2, …}) db.books.createIndex({title:1, author:-1})
Show all indexes in a collection db.collection.getIndexes() db.books.getIndexes()
Drop an index db.collection.dropIndex({indexField:type}) db.books.dropIndex({author:-1})
Show index statistics db.collection.stats() db.books.stats()

Cursor methods

To do this Run this command Example
Show number of documents in the collection cursor.count() db.books.find().count()
Limit the number of documents to return cursor.limit() db.books.find().limit(2)
Return the result set after skipping the first n number of documents cursor.skip() db.books.find().skip(2)
Sort the documents in a result set in ascending or descending order of field values cursor.sort( <{field : value}> ) value = 1 for ascending, -1 for descending db.books.find().sort({title : 1})
Display formatted (more readable) result cursor.pretty() db.books.find({}).pretty()

Comparison operators

To do this Run this command Example
equals to {: { $eq: }} db.books.find({year: {$eq: 2016}})
less than {: { $lt: }} db.books.find({year: {$lt: 2010}})  
less than or equal to {: { $lte: }} db.books.find({year: {$lte: 2008}})
greater than {: { $gt: }} db.books.find({year: {$gt: 2014}})
greater than or equal to {: { $gte: }} db.books.find({year: {$gte: 2008}})
not equal to {: { $ne: }} db.books.find({year: {$ne: 2008}})
value in {: { $in: [ , , ... }} db.books.find({year: {$in: [2008, 2016]}})
value not in {: { $nin: [ , , ... }} db.books.find({year: {$nin: [2008, 2016]}})

Logical operators

To do this Run this command Example
OR { $or: [, ,...]} db.books.find( { $or: [{year: {$lte: 2008}}, {year: {$eq: 2016}}]} )
AND { $and: [, ,...]} db.books.find( { $and: [{year: {$eq: 2008}}, {category: {$eq: “Fiction”}}]} )
NOT { $not: {}} db.books.find( {$not: {year: {$eq: 2016} }})
NOR { $nor: [, ,...]} db.books.find( { $nor: [{year: {$lte: 2008}}, {year: {$eq: 2016}}]} )

Element operators

To do this Run this command Example
Match documents that contains that specified field {: {$exists:true}} db.books.find( {category: {$exists: true }})
Match documents whose field value is of the specified BSON data type {: {$type:value}} db.books.find( {category: {$type: 2 }})

MongoDB vs SQL syntax

Terminology and Concepts

Table Collection
Row Document
Column Field
Joins Embedded documents, linking


MongoDB with rails

When you set up your initial Rails project make sure to skip active record.

$ rails new my_app --skip-active-record --skip-test --skip-system-test

Add mongoid and bson to the Gemfile

gem 'mongoid'

ps: the gem ‘bson_ext’ is no longer necessary

run mongoid installer

$ rails g mongoid:config