Sie sind auf Seite 1von 8

3/24/2020 MongoDB Cheat Sheet - Essential MongoDB Shell Commands

Tech Guides
OPEN

Home How To AskOTG Tutorials Certifications Contact Us

MongoDB Cheat Sheet - Essential


MongoDB Shell Commands
Posted on 05th December 2016

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

Select or switch use <database


use mydb
database [1] name>

Execute a
load(< lename>) load (myscript.js)
JavaScript le

Display help help help

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

Display help on db.mycol.help() db.mycol.help()


Collection

Show Commands
Show all databases show dbs show dbs

Show all
collections in show collections show collections
current database

Show all users on


show users show users
current database

Show all roles on


show roles show roles
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})

Insert multiple db.collection.insert db.books.insertMa


documents into a Many([ ny( [{"isbn":
collection <document1>, 9780198321668,
<document2>, ... ]) "title": "Romeo and
-or- Juliet", "author":
"William
db.collection.insert
Shakespeare",
([ <document1>,
"category":
<document2>, ... ])
"Tragedy", "year":
2008}, {"isbn":
9781505297409,
"title": "Treasure
Island", "author":
"Robert Louis
Stevenson",

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

Filter documents db.books. nd({"titl


db.collection. nd(<
by eld value e":"Treasure
query>)
condition Island"})

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})

Show the rst


db.collection. ndO
document that db.books. ndOne(
ne(<query>,
matches the query {}, {_id:false})
<projection>)
condition

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

Remove certain db.collection.upda db.books.update({t


elds of a single te(<query>, itle : "Treasure
document the <update>) Island"}, {$unset :
query condition {category:""}})

Remove certain db.books.update({


db.collection.upda
elds of all category :
te(<query>,
documents that "Fiction"}, {$unset :
<update>,
match the query {category:""}},
{multi:true} )
condition {multi:true})

Delete a single db.books.remove({


db.collection.remo
document that title :"Treasure
ve(<query>,
match the query Island"},
{justOne:true})
condition {justOne:true})

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})
...})

Show all indexes in db.collection.getIn db.books.getIndex


https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 4/8
3/24/2020 MongoDB Cheat Sheet - Essential MongoDB Shell Commands

a collection dexes() es()

db.collection.dropI
db.books.dropInde
Drop an index ndex(
x({author:-1})
{indexField:type} )

Show index db.collection.stats(


db.books.stats()
statistics )

Cursor Methods
Show number of
db.books. nd().cou
documents in the cursor.count()
nt()
collection

Limit the number


db.books. nd().limi
of documents to cursor.limit(<n>)
t(2)
return

Return the result


set after skipping db.books. nd().ski
cursor.skip(<n>)
the rst n number p(2)
of documents

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}})

{< eld>: { $lt: db.books. nd({yea


less than
<value> }} r: {$lt: 2010}})

less than or equal {< eld>: { $lte: db.books. nd({ye


to <value> }} ar: {$lte: 2008}})

greater than {< eld>: { $gt: db.books. nd({yea


https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 5/8
3/24/2020 MongoDB Cheat Sheet - Essential MongoDB Shell Commands

<value> }} r: {$gt: 2014}})

greater than or {< eld>: { $gte: db.books. nd({yea


equal to <value> }} r: {$gte: 2008}})

{< eld>: { $ne: db.books. nd({yea


not equal to
<value> }} r: {$ne: 2008}})

{< eld>: { $in: [ db.books. nd({yea


value in <value1>, r: {$in: [2008,
<value2>, ... }} 2016]}})

{< eld>: { $nin: [ db.books. nd({yea


value not in <value1>, r: {$nin: [2008,
<value2>, ... }} 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 }})

Match documents {< eld>: db.books. nd(


whose eld value {$type:value}} {category: {$type:
2 }})

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

[1] Databases are created on the y and will actually be created


when you insert something into it.

[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

@89rtE | October 2, 2017 3:30 PM |


Lots of HW, GZ. THX.
Reply

Shy Tamir | December 29, 2016 10:46 AM |


You might want to add this little gem for validation of data les. I use
it on our restored daily backup to verify it's usable:
db.getSiblingDB("admin").runCommand("listDatabases").databases.fo
rEach(function(d)
{db.getSiblingDB(d.name).getCollectionNames().forEach(function(c)

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

Copyright 2020 Open Tech Guides. All rights reserved

https://www.opentechguides.com/how-to/article/mongodb/118/mongodb-cheatsheat.html 8/8

Das könnte Ihnen auch gefallen