MongoDB Guide

MongoDB shell version: 2.4.9

May 29th, 2016

The Shell commands


  • mongo To enter in the MongoDB Shell
  • quit() To quit from the MongoDB Shell

Authentication

  • use admin To access the administration database
  • db.auth("admin", "password") Authenticates the admin user with its credentials

Database Management

  • db Displays the current database
  • show dbs Lists all the databases in the system
  • use mydb Accesses the database mydb
  • show collections Print out all the collections of the current database

Insert

  • db.mycollection.save({ "name" : "Matt" })
    Inserts a new Document in the collection mycollection

Find

  • db.mycollection.find()
    Retrieves all the Documents in the collection mycollection of the current database
  • db.mycollection.find().pretty()
    Formats the result in a better way
  • db.mycollection.find({ "name" : "Matt" })
    Retrieves only the documents with the field name equals to Matt
  • db.mycollection.find({ "_id" : ObjectId("z35234g5") })
    Find by ID
  • db.mycollection.find({ "age" : { "$lt" : 24 } })
    Field age less then 24
  • db.mycollection.find({ "age" : { "$lte" : 24 } })
    Field age less or equals to 24
  • db.mycollection.find({ "age" : { "$gt" : 24 } })
    Field age greater then 24
  • db.mycollection.find({ "age" : { "$gte" : 24 } })
    Field age greater or equals to 24
  • db.mycollection.find({ "name" : "Matt", "surname" : "Crow" })
    Both the fields name equals to Matt and surname equals to Crow
  • db.mycollection.find({ "$and" : [ { "name" : "Matt" }, { "surname" : "Crow" } ] })
    Both the fields name equals to Matt and surname equals to Crow
  • db.mycollection.find({ "$or" : [ { "name" : "Matt" }, { "name" : "Angus" } ] })
    Field name equals to Matt or Angus
  • db.mycollection.find({ "name" : { "$in" : [ "Matt", "Angus" ] } })
    Field name equals to Matt or Angus
  • db.mycollection.find({ "myarray" : { "$in" : [ "Element1", "Element2" ] } })
    Array myarray contains Element1 or Element2
  • DBQuery.shellBatchSize = 300
    To set the maximum size of the result (number of documents)

Remove

  • db.mycollection.remove({ "_id" : ObjectId("z35234g5") })
    Removes the document by ID
  • db.mycollection.drop()
    Deletes the collection mycollection from the current database
  • db.dropDatabase()
    Deletes the current database

Update

  • db.mycollection.update({ "_id" : ObjectId("z35234g5") }, { "$set" : { "age" : 25 } })
    Finds the document by ID and updates the field age


Export/Import


Not in the MongoDB Shell, but directly in Bash:

  • export LC_ALL="en_US.UTF-8" preliminary setup
  • mongodump --db mydb --out backup_folder/ to export the whole database mydb
  • mongorestore --db mydb backup_folder to import the database mydb from the backup folder


Connect RoboMongo to a database hosted on a Vagrant machine


  1. vagrant ssh To log in the virtual machine
  2. nano /etc/mongodb.conf To edit the configurations of mongodb
  3. Comment out the bind_ip line

    #bind_ip = 127.0.0.1