Skip to content

Instantly share code, notes, and snippets.

@opchaves
Last active September 10, 2022 21:51
Show Gist options
  • Save opchaves/e5e1dc71847220203c2dd71bdac7d306 to your computer and use it in GitHub Desktop.
Save opchaves/e5e1dc71847220203c2dd71bdac7d306 to your computer and use it in GitHub Desktop.
MongoDB Cheat Sheet

MongoDB Cheat Sheet

Connect to a database after starting mongo client

$ mongo
use dbname

Call this command before using a specific database. This command also creates the database, but the new database is only save when you insert the first document in a collection.

Connect to a particular database when starting mongo client

$ mongo dbname

Drop a particular database

use dbname
db.dropDatabase()

List all databases

show databases

List all collections of a database

use dbname
show collections

Get the status of a particular database

db.status()

As of version 3.2.10, this commands lists an object like the following:

{
  "db" : "dbname",
  "collections" : 0,
  "objects" : 0,
  "avgObjSize" : 0,
  "dataSize" : 0,
  "storageSize" : 0,
  "numExtents" : 0,
  "indexes" : 0,
  "indexSize" : 0,
  "fileSize" : 0,
  "ok" : 1
}

By default the data size will show up in bytes, but if you want it in KB, use this db.stats(1024). For MB, use db.stats(1024*1024)

List the current connections to a mongodb server

db.serverStatus().connections

CRUD Operations

Insert a document

db.movies.insertOne({"title":"New Movie", "year":2010, "imdb": "aa0099999"})

This command inserts a document into a movies collection

Count the number of documents in a given collection

db.movies.count()

List documents in a collection without specifying any constraints

db.movies.find()
// or if you want to format the output
db.movies.find().pretty()

List documents in a collection using a cursor

// The mongo shell talks JavaScript
var cs = db.movies.find()
cs.hasNext() // checks whether or not there is a document to show
cs.next() // outputs the next object
// You could keep on doing this until you exaust the cursor, but it is problably not a good idea if the queries returns thousands of documents. rsrsrsr
// This techinique is very useful when working with a driver such as the Java driver

Finding and Sorting

  • sort({field: 1|-1}): 1 = ascending; -1 = descending

List subdocuments ordered by subdoc.field. It returns only a list with 10 docs, leaving out all other fields, even _id.

db.collection.find({},{_id:null, "subdoc.field":1}).sort({"subdoc.field": -1}).limit(10).pretty()

Aggregation

// things collection
{
  name: "John",
  country: "US",
  children: [{
    name: "Rachel",
    age: 10,
    watched_movies: [{
      title: "movie 1"
    },
    {
      title: "movie 2"
    }]
  }]
}

Given the above document, how would you get the number of watched movies by John's children?

db.things.aggregate([
	{ $match: { name: "John" } },
	{ $unwind: "$children" },
	{ $unwind: "$children.watched_movies" },
	{ $group: { _id: null, count: { $sum: 1 } } }
])

Get a cursor, add all documents in an array and call tojson() to print them

records = [];
var cursor = db.someCollection.find({}, {}).limit(100);
while(cursor.hasNext()) {
    records.push(cursor.next())
    //var c = cursor.next();
    //print(c.field1 + ',' + c.field2 + ',' + c.field3)
}
print(tojson(records))

Dump, Import, Export

Import a JSON file containing a collection of companies into a database called mclass

sudo mongoimport --db mclass --collection companies --file companies.json

Useful links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment