mirror of
https://github.com/Fechin/reference.git
synced 2026-11-21 05:24:10 +03:00
Added the alternative methods for the deprecated methods
This commit is contained in:
1 parent
8d6bc1d68f
commit
4079d45622
1 file changed
+28
-36
+28
-36
@@ -70,56 +70,55 @@ load("myScript.js")
|
||||
|
||||
```mongosh
|
||||
db.coll.insertOne({name: "Max"})
|
||||
db.coll.insert([{name: "Max"}, {name:"Alex"}]) // ordered bulk insert
|
||||
db.coll.insert([{name: "Max"}, {name:"Alex"}], {ordered: false}) // unordered bulk insert
|
||||
db.coll.insert({date: ISODate()})
|
||||
db.coll.insert({name: "Max"}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})
|
||||
db.coll.insertMany([{name: "Max"}, {name:"Alex"}]) // ordered bulk insert
|
||||
db.coll.insertMany([{name: "Max"}, {name:"Alex"}], {ordered: false}) // unordered bulk insert
|
||||
db.coll.insertOne({date: ISODate()})
|
||||
db.coll.insertMany({name: "Max"}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})
|
||||
```
|
||||
|
||||
### Delete
|
||||
|
||||
```mongosh
|
||||
db.coll.remove({name: "Max"})
|
||||
db.coll.remove({name: "Max"}, {justOne: true})
|
||||
db.coll.remove({}) // WARNING! Deletes all the docs but not the collection itself and its index definitions
|
||||
db.coll.remove({name: "Max"}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})
|
||||
db.coll.deleteOne({name: "Max"})
|
||||
db.coll.deleteMany( $and: [{name: "Max"}, {justOne: true}]) //delete all entries which contain both values
|
||||
db.coll.deleteMany( $or: [{name: "Max"}, {justOne: true}]) //delete all entries which contain any of the specified values
|
||||
db.coll.deleteMany({}) // WARNING! Deletes all the docs but not the collection itself and its index definitions
|
||||
db.coll.deleteMany({name: "Max"}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})
|
||||
db.coll.findOneAndDelete({"name": "Max"})
|
||||
```
|
||||
|
||||
### Update
|
||||
|
||||
```mongosh
|
||||
db.coll.update({"_id": 1}, {"year": 2016}) // WARNING! Replaces the entire document
|
||||
db.coll.update({"_id": 1}, {$set: {"year": 2016, name: "Max"}})
|
||||
db.coll.update({"_id": 1}, {$unset: {"year": 1}})
|
||||
db.coll.update({"_id": 1}, {$rename: {"year": "date"} })
|
||||
db.coll.update({"_id": 1}, {$inc: {"year": 5}})
|
||||
db.coll.update({"_id": 1}, {$mul: {price: NumberDecimal("1.25"), qty: 2}})
|
||||
db.coll.update({"_id": 1}, {$min: {"imdb": 5}})
|
||||
db.coll.update({"_id": 1}, {$max: {"imdb": 8}})
|
||||
db.coll.update({"_id": 1}, {$currentDate: {"lastModified": true}})
|
||||
db.coll.update({"_id": 1}, {$currentDate: {"lastModified": {$type: "timestamp"}}})
|
||||
db.coll.updateMany({"_id": 1}, {$set: {"year": 2016}}) // WARNING! Replaces the entire document where "_id" = 1
|
||||
db.coll.updateOne({"_id": 1}, {$set: {"year": 2016, name: "Max"}})
|
||||
db.coll.updateOne({"_id": 1}, {$unset: {"year": 1}})
|
||||
db.coll.updateOne({"_id": 1}, {$rename: {"year": "date"} })
|
||||
db.coll.updateOne({"_id": 1}, {$inc: {"year": 5}})
|
||||
db.coll.updateOne({"_id": 1}, {$mul: {price: 2}})
|
||||
db.coll.updateOne({"_id": 1}, {$min: {"imdb": 5}})
|
||||
db.coll.updateOne({"_id": 1}, {$max: {"imdb": 8}})
|
||||
db.coll.updateMany({"_id": {$lt: 10}}, {$set: {"lastModified": ISODate()}})
|
||||
```
|
||||
|
||||
### Array {.row-span-2}
|
||||
|
||||
```mongosh
|
||||
db.coll.update({"_id": 1}, {$push :{"array": 1}})
|
||||
db.coll.update({"_id": 1}, {$pull :{"array": 1}})
|
||||
db.coll.update({"_id": 1}, {$addToSet :{"array": 2}})
|
||||
db.coll.update({"_id": 1}, {$pop: {"array": 1}}) // last element
|
||||
db.coll.update({"_id": 1}, {$pop: {"array": -1}}) // first element
|
||||
db.coll.update({"_id": 1}, {$pullAll: {"array" :[3, 4, 5]}})
|
||||
db.coll.update({"_id": 1}, {$push: {scores: {$each: [90, 92, 85]}}})
|
||||
db.coll.updateOne({"_id": 1}, {$push :{"array": 1}})
|
||||
db.coll.updateOne({"_id": 1}, {$pull :{"array": 1}})
|
||||
db.coll.updateOne({"_id": 1}, {$addToSet :{"array": 2}})
|
||||
db.coll.updateOne({"_id": 1}, {$pop: {"array": 1}}) // last element
|
||||
db.coll.updateOne({"_id": 1}, {$pop: {"array": -1}}) // first element
|
||||
db.coll.updateOne({"_id": 1}, {$pullAll: {"array" :[3, 4, 5]}})
|
||||
db.coll.updateOne({"_id": 1}, {$push: {scores: {$each: [90, 92, 85]}}})
|
||||
db.coll.updateOne({"_id": 1, "grades": 80}, {$set: {"grades.$": 82}})
|
||||
db.coll.updateMany({}, {$inc: {"grades.$[]": 10}})
|
||||
db.coll.update({}, {$set: {"grades.$[element]": 100}}, {multi: true, arrayFilters: [{"element": {$gte: 100}}]})
|
||||
db.coll.updateMany({}, {$set: {"grades.$[element]": 100}}, {arrayFilters: [{"element": {$gte: 100}}]})
|
||||
```
|
||||
|
||||
### 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"}})
|
||||
```
|
||||
|
||||
@@ -132,7 +131,7 @@ db.coll.findOneAndUpdate({"name": "Max"}, {$inc: {"points": 5}}, {returnNewDocum
|
||||
### Upsert {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.update({"_id": 1}, {$set: {item: "apple"}, $setOnInsert: {defaultQty: 100}}, {upsert: true})
|
||||
db.coll.updateOne({"_id": 1}, {$set: {item: "apple"}, $setOnInsert: {defaultQty: 100}}, {upsert: true})
|
||||
```
|
||||
|
||||
### Replace {.row-span-1}
|
||||
@@ -141,16 +140,10 @@ db.coll.update({"_id": 1}, {$set: {item: "apple"}, $setOnInsert: {defaultQty: 10
|
||||
db.coll.replaceOne({"name": "Max"}, {"firstname": "Maxime", "surname": "Beugnet"})
|
||||
```
|
||||
|
||||
### Save {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.save({"item": "book", "qty": 40})
|
||||
```
|
||||
|
||||
### Write concern {.row-span-1}
|
||||
|
||||
```mongosh
|
||||
db.coll.update({}, {$set: {"x": 1}}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})
|
||||
db.coll.updateMany({}, {$set: {"x": 1}}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})
|
||||
```
|
||||
|
||||
### Find {.row-span-2}
|
||||
@@ -168,7 +161,6 @@ db.coll.distinct("name")
|
||||
### Count
|
||||
|
||||
```mongosh
|
||||
db.coll.count({age: 32}) // estimation based on collection metadata
|
||||
db.coll.estimatedDocumentCount() // estimation based on collection metadata
|
||||
db.coll.countDocuments({age: 32}) // alias for an aggregation pipeline - accurate count
|
||||
```
|
||||
|
||||
Reference in new issue
Block a user