mirror of
https://github.com/Fechin/reference.git
synced 2026-11-21 21:44:45 +03:00
fix: mongodb format
Fixed a small capitalization and the class modifier things
This commit is contained in:
1 file changed
+25
-23
+25
-23
@@ -1,13 +1,16 @@
|
||||
---
|
||||
title: MongoDB
|
||||
date: 2023-04-05
|
||||
background: bg-gradient-to-r from-green-900 via-green-600 to-green-400 hover:from-green-900 hover:via-green-700 hover:to-green-500
|
||||
background:
|
||||
bg-gradient-to-r from-green-900 via-green-600 to-green-400 hover:from-green-900 hover:via-green-700 hover:to-green-500
|
||||
tags:
|
||||
- NoSQL
|
||||
- DB
|
||||
categories:
|
||||
- Database
|
||||
intro: The MongoDB cheat sheet provides you with the most commonly used MongoDB commands and queries for your reference. the cheatsheet is from mongodb developers website
|
||||
intro:
|
||||
The MongoDB cheat sheet provides you with the most commonly used MongoDB commands and queries for your reference. the
|
||||
cheatsheet is from mongodb developers website
|
||||
plugins:
|
||||
- tooltip
|
||||
- copyCode
|
||||
@@ -35,7 +38,7 @@ mongo "mongodb+srv://cluster-name.abcde.mongodb.net/<dbname>" --username <userna
|
||||
|
||||
### Helpers
|
||||
|
||||
show dbs :
|
||||
Show dbs :
|
||||
|
||||
```mongosh
|
||||
db // prints the current database
|
||||
@@ -98,7 +101,7 @@ db.coll.update({"_id": 1}, {$currentDate: {"lastModified": true}})
|
||||
db.coll.update({"_id": 1}, {$currentDate: {"lastModified": {$type: "timestamp"}}})
|
||||
```
|
||||
|
||||
### Array { .row-span-2 }
|
||||
### Array {.row-span-2}
|
||||
|
||||
```mongosh
|
||||
db.coll.update({"_id": 1}, {$push :{"array": 1}})
|
||||
@@ -113,44 +116,44 @@ db.coll.updateMany({}, {$inc: {"grades.$[]": 10}})
|
||||
db.coll.update({}, {$set: {"grades.$[element]": 100}}, {multi: true, arrayFilters: [{"element": {$gte: 100}}]})
|
||||
```
|
||||
|
||||
### Update many { .row-span-1 }
|
||||
### Update many {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.update({"year": 1999}, {$set: {"decade": "90's"}}, {"multi":true})
|
||||
db.coll.updateMany({"year": 1999}, {$set: {"decade": "90's"}})
|
||||
```
|
||||
|
||||
### FindOneAndUpdate { .row-span-1 }
|
||||
### FindOneAndUpdate {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.findOneAndUpdate({"name": "Max"}, {$inc: {"points": 5}}, {returnNewDocument: true})
|
||||
```
|
||||
|
||||
### Upsert { .row-span-1 }
|
||||
### Upsert {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.update({"_id": 1}, {$set: {item: "apple"}, $setOnInsert: {defaultQty: 100}}, {upsert: true})
|
||||
```
|
||||
|
||||
### Replace { .row-span-1 }
|
||||
### Replace {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.replaceOne({"name": "Max"}, {"firstname": "Maxime", "surname": "Beugnet"})
|
||||
```
|
||||
|
||||
### Save { .row-span-1 }
|
||||
### Save {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.save({"item": "book", "qty": 40})
|
||||
```
|
||||
|
||||
### Write concern { .row-span-1 }
|
||||
### Write concern {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.update({}, {$set: {"x": 1}}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})
|
||||
```
|
||||
|
||||
### Find { .row-span-2 }
|
||||
### Find {.row-span-2}
|
||||
|
||||
```mongosh
|
||||
db.coll.findOne() // returns a single document
|
||||
@@ -255,17 +258,16 @@ db.coll.find({}).sort({"year": 1, "rating": -1}).skip(10).limit(3)
|
||||
db.coll.find().readConcern("majority")
|
||||
```
|
||||
|
||||
## Databases and Collections {.cols-2}
|
||||
|
||||
## Databases and Collections { .cols-2 }
|
||||
|
||||
### Drop { .row-span-1 }
|
||||
### Drop {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.drop() // removes the collection and its index definitions
|
||||
db.dropDatabase() // double check that you are *NOT* on the PROD cluster... :-)
|
||||
```
|
||||
|
||||
### Create Collection { .row-span-2}
|
||||
### Create Collection {.row-span-2}
|
||||
|
||||
```mongosh
|
||||
db.createCollection("contacts", {
|
||||
@@ -291,7 +293,7 @@ db.createCollection("contacts", {
|
||||
})
|
||||
```
|
||||
|
||||
### Other Collection Functions { .row-span-1}
|
||||
### Other Collection Functions {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.stats()
|
||||
@@ -302,7 +304,6 @@ db.coll.validate({full: true})
|
||||
db.coll.renameCollection("new_coll", true) // 2nd parameter to drop the target collection if exists
|
||||
```
|
||||
|
||||
|
||||
## Indexes {.cols-2}
|
||||
|
||||
### Basics
|
||||
@@ -348,9 +349,9 @@ db.coll.createIndex({"name": 1}, {collation: {locale: 'en', strength: 1}}) //
|
||||
db.coll.createIndex({"name": 1 }, {sparse: true})
|
||||
```
|
||||
|
||||
## Others { .cols-2 }
|
||||
## Others {.cols-2}
|
||||
|
||||
### Handy commands { .row-span-3 }
|
||||
### Handy commands {.row-span-3}
|
||||
|
||||
```mongosh
|
||||
use admin
|
||||
@@ -393,7 +394,7 @@ db.getFreeMonitoringStatus()
|
||||
db.createView("viewName", "sourceColl", [{$project:{department: 1}}])
|
||||
```
|
||||
|
||||
### Replica Set { .row-span-2}
|
||||
### Replica Set {.row-span-2}
|
||||
|
||||
```mongosh
|
||||
rs.status()
|
||||
@@ -415,7 +416,7 @@ rs.slaveOk()
|
||||
rs.stepDown(20, 5) // (stepDownSecs, secondaryCatchUpPeriodSecs)
|
||||
```
|
||||
|
||||
### Sharded Cluster { .row-span-2 }
|
||||
### Sharded Cluster {.row-span-2}
|
||||
|
||||
```mongosh
|
||||
sh.status()
|
||||
@@ -445,7 +446,8 @@ sh.addShardToZone("shard0000", "JFK")
|
||||
sh.removeShardFromZone("shard0000", "NYC")
|
||||
sh.removeRangeFromZone("mydb.coll", {a: 1, b: 1}, {a: 10, b: 10})
|
||||
```
|
||||
### Change Streams {.row-span-1 }
|
||||
|
||||
### Change Streams {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
watchCursor = db.coll.watch( [ { $match : {"operationType" : "insert" } } ] )
|
||||
@@ -455,4 +457,4 @@ while (!watchCursor.isExhausted()){
|
||||
print(tojson(watchCursor.next()));
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
Reference in new issue
Block a user