Skip to content

Instantly share code, notes, and snippets.

@cecyc
Last active May 15, 2017 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecyc/e9279e2bd92fb9a184b7 to your computer and use it in GitHub Desktop.
Save cecyc/e9279e2bd92fb9a184b7 to your computer and use it in GitHub Desktop.
Mongo DB cheat sheet

MongoDB cheat sheet

For those of you learning Mongo and need a quick reference.

Show databases on the server

show dbs

Use a specific db on the server

use <db>

Show "tables" In Mongo, you have collections, not tables. To view the collections available:

show collections

Basic search

db.collection.find({"key": "value"})

Cursor

Your cursor is essentially the query you're using to search for something. You can set it to a variable as a shortcut as well:

var user = db.users.find(<OPTIONS>);

Search by Object ID IDs are a bit diferent than just an ID, it is an ObjectId (ObjectId("555555555555")). ID keys will always be "_id".

db.users.find({"_id": ObjectId("55555555")})

Simple updates to one document

The structure for basic updates with .update is:

  1. query (find what you want to update),
  2. the update itself, using $set, then options.

db.users.update({"_id": ObjectId("555555")}, {$set: {"name": "Rusty"}})

**Updates for multiple documents

For updates on multiple documents, you can use {multi: true} to update multiple documents. If not defined, multi will be set to "false" (meaning Mongo will only update the first document it finds then quit).

You can also use upsert: true, which will create a document if one doesn't exist. USE THIS SPARINGLY, obviously, otherwise you may end up creating documents you don't need. Only use upsert: true if you are absolutely sure you need to create documents if one does not already exist. If not defined, upsert is set to false (meaning a new document is not created).

Example: Update all users in the users collection whose location is "Austin" and set it to "Portland". This update should apply to multiple users, but it should not create a document if one does not exist.

db.users.find({"location" {$in: "Austin"}}, {$set: "Portland"}, {multi: true})

Note we're using multi: true so this applies to all users who meet the criteria. No need to specify upsert: false since it is false by default.

Includes vs. Exists

Includes ($in) will search for documents where a certain field includes a certain value.

BE CAREFUL not to use $inc, which is increment.

You can also use a regex with your includes statement: $in: /some regex/. (Note there is a $regex operator, but it cannot be used with $in)

Exists ($exists) looks for a certain field to exist in a document. A document store is flexible, and often, some documents don't include certain fields if a document doesn't need it. Sometimes, you will want to search documents to see if a certain field exists in the document: db.users.find({"admin": {$exists: true}}).

So, $in searches for values in a field that exists, and $exists searches for the existence of a field in a document.

Other handy things

.count() will count documents matching your query.

.pretty() will output nicely formatted JSON.

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