Cheatography
https://cheatography.com
Basic Mongo DB
db
|
Show name of current database |
mongod
|
Start database |
mongo
|
Connect to database |
show dbs
|
Show databases |
use db
|
Switch to database db |
show collections
|
Display current database collections |
Create
insert(data)
|
insert document(s) returns write result |
insertOne (data, options)
|
insert one document |
insertMany(data, options)
|
insert many documents |
insertMany([{},{},{}])
|
needs square brackets |
Read
db.collection.find()
|
Display documents from collection |
find(filter, options)
|
find all matching documents |
findOne(filter, options)
|
find first matching document |
Update
updateOne(filter, data, options)
|
Change one document |
updateMany(filter, data, options)
|
Change many documents |
replaceOne(filter, data, options)
|
Replace document entirely |
Delete
deleteOne(filter, options)
|
Delete one document |
deleteMany(filter, options)
|
Delete many documents |
|
|
Filters
{"key": "value"}
|
Used for filter arguments to filter collection |
{key: {$operator: value} }
|
Operators for querying data |
{key: {$exists: true}}
|
Matches all documents containing subdocument key |
$eq
|
Matches values that are equal to a specified value. |
$gt
|
Matches values that are greater than a specified value. |
$gte
|
Matches values that are greater than or equal to a specified value. |
$in
|
Matches any of the values specified in an array |
syntax:
|
{key:{$in: [array of values] } }
|
$lt
|
Matches values that are less than a specified value. |
$lte
|
Matches values that are less than or equal to a specified value. |
$ne
|
Matches all values that are not equal to a specified value. |
$nin
|
Matches none of the values specified in an array. |
$and
|
Performs AND operation |
syntax:
|
{$and: [ {},{} ] }
|
{key: {$op: filter}, {filter}}
|
$and operator is necessary when the same field or operator has to be specified in multiple expressions |
find({doc.subdoc:value})
|
Filter sub documents |
Functions
.count()
|
Counts how many results |
.sort(filter)
|
Sort ascend:1 descend: -1 |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets