Mongodb Shell Commands Cheat Sheet



View MongoDBShell.pdf from CIS MISC at Saint Leo University. MongoDB Shell Cheat Sheet Starting the mongo shell Mongo shell can be used to connect to local databases, or remote databases running. As part of our MongoDB Guide, we’ve compiled this cheat sheet of common and not-so-common MongoDB commands. (This article is part of our MongoDB Guide. Use the right-hand menu to navigate.) Table of Contents. Pretty Print Create Collection Create Indexes Create index Create sparse index Create compound index Create geo index Create partial index.

This is a Cheat Sheet for interacting with the Mongo Shell ( mongo on your command line). This is for MongoDB Community Edition.

Preface:

This article can serve as a “cheat sheet” of sorts, discussing some of the most commonly-used MongoDB database commands. These essential commands enable users to interact with a MongoDB database to create collections and documents. MongoDb Commands Cheatsheet. Following are the MongoDB commands which can be used as a cheat sheet to help you get up and running with the platform: Start and stop the MongoDB Database: sudo service mongod start and sudo service mongod stop. Access the MongoDB database using Shell: mongo –host localhost:27017; Show all databases: show dbs. Mongodb cheat sheet. MongoDB Commands Cheat Sheet for Beginners, This article includes information on MongoDB commands and gives a sort of cheat sheet that beginners can use to make things easier. MongoDB Cheat Sheat - A quick reference of Mongo shell commands and usage example needed to perform most of the common tasks in MongoDB.

Mongo Manual can help you with getting started using the Shell.

FAQ for MongoDB Fundamentals and other FAQs can be found in the side-bar after visiting that link.

Algebra iimath problem solving. The Mongo Shell reference can be found here.

Anything that looks like db.<something>() will be found in Database Methods.

Anything that looks like db.collection.<something> will be found in Collection Methods.

Use db.help() to get a list of all database commands. ( Note that it's pretty long.)

Anything in < > should be replaced with your unique values. IE: You want to have a database called Cars so you would use the command use <db> but you would type it as use Cars.

Databases:

This table will list the commands most commonly used when working with the database as a whole.

Mongodb
TypeCommandDescription
Create/Connectuse <db>Connects to a specific database. If none exists then one will automatically be created with that name. Doc
List Allshow dbsLists all Databases. DBs with no data are not shown. Doc
List Currentdb.getName()Lists the name of the currently selected databasse. Doc
ReturndbReturns the currently seleceted Database. Allows you to use methods and chain commands. IE db.createCollection('test'). Doc
Dropdb.dropDatabase()Drops the currently selected Database. Doc
Statsdb.stats()Lists the stats about the current Database. Doc

Collections:

This Table lists the commands most commonly used when working with collections as a whole.

TypeCommandDescription
Createdb.createCollection('<collection>)Creates a new empty collection in the database. Doc
Listdb.getCollectionNames()Lists all collections for a current Database. Doc
Returndb.getCollection('<collection>')Returns a collection. Can chain methods onto it. IE db.getCollection.('authors').find({}). Doc
Return/Createdb.<collection>Similar to db.getCollection(), but if the collection doesn't exist, then the collection will be return, but not added unless data is also added to that collection at the same time. IE: db.items does not add a collection to the database, but db.items.insertOne({name: 'Mike'}) would, because data is also being added to the collection. Use db.createCollection() to add an empty collection. Doc
Dropdb.<collection>.drop()Drops the collection from the database. Doc
Renamedb.<cllectn>.renameCollection('collection')Renames a collection. Doc

Data

This section is broken down into 5 sub-sections. The first 4 match the CRUD Verbs of Create, Read, Update, Delete, and the last one is a list of Combos like findAndUpdate. Check here for the Mongo Docs on Crud

Couple notes here:

  1. All of these commands start with db.<collection> where <collection> is the name of the collection you want to call these methods on. Any exceptions will be noted in the description.**
  2. {query} Is referring to queries like {<field>: '<value>', <field>:'<value>', etc). IE: { book: 'Golden River Doth Flows', author: 'I.P. Freely', etc}. They are field-value pairs that are used to make documents, and also used during searches, aka queries.

Create

TypeCommandDescription
Create One.insertOne({<doc>})Inserts a document into the collection. IE: db.cars.insertOne({make:'ford'}). Doc
Create One/Many.insert([{<doc>},{<doc>},{<doc>}] )Inserts One or more documents into the collection. If an array is passed in it will make a record of each document in the array. Otherwise it will accept a single document. Doc

Read

Command

Most of these commands allow methods to be chained onto them.

Commands

Mongodb Shell Commands Cheat Sheet Pdf

TypeCommandDescription
Count.count()Returns the number of items in the collection. Doc
Return All.find({})Returns an array of all items in a collection. .find() Must be passed {} in order to return all documents. Doc
Return All Filtered.find({query})Returns an array of items in a collection that match the query passed into .find(). See Query and Project Operators for extra info on querys. Doc
Return One Filtered.findOne({query})Returns a document (not an array) of the first item found, filtering based off what was passed into .findOne(). Useful when searching by unique fields like _id, IE db.cars.findOne({_id: 1}). Doc

Update

TypeCommandDescription
Update/Replace.update({query}, { $set: {query} }, options)The first argument is used as the query to find the document. The second argument specifies which field which to update. Exclude $set: and the entire document will be Replaced. Common options: upsert: <boolean> to keep it unique, and multi: <boolean> will update multiple documents if set to true. Docs and Field Operator Docs
Update One/Many.updateOne() and .updateMany()Basically the same as the above function, except the multi: <boolean> option is basically defaulted to false and true, and isnt' an allowed option that can be passed in. One Doc, Many Doc

Delete

TypeCommandDescription
Delete One.deleteOne({query})Deletes the first document that matches the query. Recommend to search by _id or another unique field. Doc
Delete Many/All.deleteMany({query})Deletes all records that match the query. Leave the query blank to delete all documents. Doc

Combos

Mongodb Shell Commands Cheat Sheet Download

These are some combo commands that really just do the same thing as their base commands, but have a couple extra options.

Posesdaruma fields saddlery. All of these can leave their {query} blank and it will find the first document and execute it's verb on that item.

Mongodb command shell

Mongodb Shell Commands Cheat Sheet Free

CommandDesctiption
.findOneAndDelete({query})Finds the first document that matches the query and deletes it. [Doc] https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndDelete/#db.collection.findOneAndDelete
.findOneAndUpdate({query}, {<update>}, {<options>})Finds the first document that matches the query in the first argument, and updates it using the second arguments. Has optional options as well. Doc
.findOneAndReplace({query}, {<replacement>}, {<options>})Finds and replaces the document that matches the query. <replacement> cannot use update operators. Doc