Beruflich Dokumente
Kultur Dokumente
Tech Guides
OPEN
H ere are the most commonly used Mongo Shell commands with
example usage.
Basic Commands
Run this
To do this Example
command
Connect to local
host on default mongo mongo
port 27017
mongo --host
Connect to remote mongo --host
<hostname or ip
host on speci ed 10.121.65.23 --port
address> --port
port 23020
<port no>
mongo
Connect to a mongo
<host>/<database
database 10.121.65.58/mydb
>
Show current
db db
database
Execute a
load(< lename>) load (myscript.js)
JavaScript le
Display help on DB
db.help() db.help()
methods
https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 1/8
3/24/2020 MongoDB Cheat Sheet - Essential MongoDB Shell Commands
Show Commands
Show all databases show dbs show dbs
Show all
collections in show collections show collections
current database
CRUD Operations
db.books.insert({"i
sbn":
9780060859749,
Insert a new "title": "After Alice:
db.collection.insert
document in a A Novel", "author":
( <document> )
collection [2] "Gregory Maguire",
"category":
"Fiction",
"year":2016})
https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 2/8
3/24/2020 MongoDB Cheat Sheet - Essential MongoDB Shell Commands
"category":
"Fiction",
"year":2014}])
-or-
db.books.insert([{
"isbn":"978185326
0001", "title":
"Pride and
Prejudice",
"author": "Jane
Austen",
"category":
"Fiction"}, {"isbn":
"9780743273565",
"title": "The Great
Gatsby", "author":
"F. Scott
Fitzgerald"} ])
Show all
documents in the db.collection. nd() db.books. nd()
collection
db.books. nd({"titl
Show only some db.collection. nd(< e":"Treasure
elds of matching query>, Island"}, {title:true,
documents <projection>) category:true,
_id:false})
db.books.update({t
Update speci c
itle : "Treasure
elds of a single db.collection.upda
Island"}, {$set :
document that te(<query>,
{category
match the query <update> )
:"Adventure
condition
Fiction"}})
https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 3/8
3/24/2020 MongoDB Cheat Sheet - Essential MongoDB Shell Commands
Delete all
db.books.remove({
documents db.collection.remo
"category"
matching a query ve(<query>)
:"Fiction"})
condition
Delete all
db.collection.remo db.books.remove({
documents in a
ve({}) })
collection
Index
db.collection.creat
eIndex(
{indexField:type} ) db.books.createIn
Create an index
Type 1 means dex({title:1})
ascending; -1
means descending
db.collection.creat
db.books.createIn
Create a unique eIndex(
dex( {isbn:1},
index {indexField:type},
{unique:true} )
{unique:true} )
db.collection.creat
Create a index on eIndex({indexField db.books.createIn
multiple elds 1:type1, dex({title:1,
(compound index) indexField2:type2, author:-1})
...})
db.collection.dropI
db.books.dropInde
Drop an index ndex(
x({author:-1})
{indexField:type} )
Cursor Methods
Show number of
db.books. nd().cou
documents in the cursor.count()
nt()
collection
Sort the
cursor.sort( <{ eld
documents in a
: value}> )
result set in db.books. nd().sor
value = 1 for
ascending or t( {title : 1} )
ascending, -1 for
descending order
descending
of eld values
Display formatted
db.books. nd({}).p
(more readable) cursor.pretty()
retty()
result
Comparison Operators
{< eld>: { $eq: db.books. nd({yea
equals to
<value> }} r: {$eq: 2016}})
Logical Operators
db.books. nd( {
{ $or:
$or: [{year: {$lte:
OR [<expression1>,
2008}}, {year: {$eq:
<expression2>,...]}
2016}}]} )
db.books. nd( {
{ $and:
$and: [{year: {$eq:
AND [<expression1>,
2008}}, {category:
<expression2>,...]}
{$eq: "Fiction"}}]} )
db.books. nd(
{ $not:
NOT {$not: {year: {$eq:
{<expression>}}
2016} }})
db.books. nd( {
{ $nor:
$nor: [{year: {$lte:
NOR [<expression1>,
2008}}, {year: {$eq:
<expression2>,...]}
2016}}]} )
Element Operators
Match documents db.books. nd(
{< eld>:
that contains that {category:
{$exists:true}}
speci ed eld {$exists: true }})
https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 6/8
3/24/2020 MongoDB Cheat Sheet - Essential MongoDB Shell Commands
is of the speci ed
BSON data type
[2] Collections are created on the y when you insert rst document
into it.
Rate it
Related Articles
Post a comment
Your Comment....
Name Email
Post Comment
Comments
brian | July 13, 2019 2:45 AM |
awesome write up
Reply
https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 7/8
3/24/2020 MongoDB Cheat Sheet - Essential MongoDB Shell Commands
{assert(db.getSiblingDB(d.name).getCollection(c).validate().valid==true
)})})
Reply
https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 8/8