当前位置:Gxlcms > 数据库问题 > MongoDB CRUD之U

MongoDB CRUD之U

时间:2021-07-01 10:21:17 帮助过:6人阅读

.students.update( { grades: { $gte: 100 } }, { $set: { "grades.$[element]" : 100 } }, { multi: true, arrayFilters: [ { "element": { $gte: 100 } } ] } )

加载js文档

23 > load("update_students.js")
true

因为我的版本是3.4,所以不会生效

正常变更后的数据为:

{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 100 ] }
{ "_id" : 3, "grades" : [ 95, 100, 100 ] }

这是来自官网的教程 , 只能以后再看略.

updateOne

格式

db.collection.updateOne(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ]
   }
)

可以看到参数与Update一样的,但顾名思义的应该是只能更新一条数据

使用参数upsert

数据

27 > load("data_restaurant.js")
true
28 > db.restaurant.find()
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : "0" }

操作

29 > try {
...    db.restaurant.updateOne(
...       { "name" : "Pizza Rat's Pizzaria" },
...       { $set: {"_id" : 4, "violations" : 7, "borough" : "Manhattan" } },
...       { upsert: true }
...    );
... } catch (e) {
...    print(e);
... }
{
    "acknowledged" : true,
    "matchedCount" : 0,
    "modifiedCount" : 0,
    "upsertedId" : 4
}
30 > db.restaurant.find()
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : "0" }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "violations" : 7, "borough" : "Manhattan" }

将违规10次以上的餐饮进行关停

31 > try {
...    db.restaurant.updateOne(
...       { "violations" : { $gt: 10} },
...       { $set: { "Closed" : true } },
...       { upsert: true }
...    );
... } catch (e) {
...    print(e);
... }
{
    "acknowledged" : true,
    "matchedCount" : 0,
    "modifiedCount" : 0,
    "upsertedId" : ObjectId("5a79c54fcf6c4175a57b2bff")
}
32 > db.restaurant.find()
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : "0" }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "violations" : 7, "borough" : "Manhattan" }
{ "_id" : ObjectId("5a79c54fcf6c4175a57b2bff"), "Closed" : true }

updataMany

格式

db.collection.updateMany(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ]
   }
)

无参更新

39 > db.restaurant.find()
{ "_id" : 1, "name" : "Central Perk Cafe", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Sub", "violations" : 5 }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "violations" : 8 }
40 > try {
...    db.restaurant.updateMany(
...       { violations: { $gt: 4 } },
...       { $set: { "Review" : true } }
...    );
... } catch (e) {
...    print(e);
... }
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
41 > db.restaurant.find()
{ "_id" : 1, "name" : "Central Perk Cafe", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Sub", "violations" : 5, "Review" : true }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "violations" : 8, "Review" : true }

可以看到违规4次以上的餐饮将进行复查

使用参数upsert

43 > db.inspectors.find()
{ "_id" : 92412, "inspector" : "F. Drebin", "Sector" : 1, "Patrolling" : true }
{ "_id" : 92413, "inspector" : "J. Clouseau", "Sector" : 2, "Patrolling" : false }
{ "_id" : 92414, "inspector" : "J. Clouseau", "Sector" : 3, "Patrolling" : true }
{ "_id" : 92415, "inspector" : "R. Coltrane", "Sector" : 3, "Patrolling" : false }
44 > try {
...    db.inspectors.updateMany(
...       { "Sector" : { $gt : 4 }, "inspector" : "R. Coltrane" },
...       { $set: { "Patrolling" : false } },
...       { upsert: true }
...    );
... } catch (e) {
...    print(e);
... }
{
    "acknowledged" : true,
    "matchedCount" : 0,
    "modifiedCount" : 0,
    "upsertedId" : ObjectId("5a79c792cf6c4175a57b2c12")
}
45 > db.inspectors.find()
{ "_id" : 92412, "inspector" : "F. Drebin", "Sector" : 1, "Patrolling" : true }
{ "_id" : 92413, "inspector" : "J. Clouseau", "Sector" : 2, "Patrolling" : false }
{ "_id" : 92414, "inspector" : "J. Clouseau", "Sector" : 3, "Patrolling" : true }
{ "_id" : 92415, "inspector" : "R. Coltrane", "Sector" : 3, "Patrolling" : false }
{ "_id" : ObjectId("....."), "inspector" : "R. Coltrane", "Patrolling" : false }

replaceOne

格式

db.collection.replaceOne(
   <filter>,
   <replacement>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>
   }
)

这一条命令会取代原文档

无参更新

> db.restaurant.find()
{ "_id" : 1, "name" : "Central Perk Cafe", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Sub", "violations" : 5 }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "violations" : 8 }
> try {
...    db.restaurant.replaceOne(
...       { "name" : "Central Perk Cafe" },
...       { "name" : "Central Pork Cafe", "Borough" : "Manhattan" }
...    );
... } catch (e){
...    print(e);
... }
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.restaurant.find()
{ "_id" : 1, "name" : "Central Pork Cafe", "Borough" : "Manhattan" }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Sub", "violations" : 5 }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "violations" : 8 }

使用参数upsert

> db.restaurant.find()
{ "_id" : 1, "name" : "Central Perk Cafe", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Sub", "violations" : 5 }
> try {
...    db.restaurant.replaceOne(
...       { "name" : "Pizza Rat's Pizzaria" },
...       { "_id": 4, "name" : "Pizza Rat's Pizzaria", "Borough" : "Manhattan", "violations" : 8 },
...       { upsert: true }
...    );
... } catch (e){
...    print(e);
... }
{
    "acknowledged" : true,
    "matchedCount" : 0,
    "modifiedCount" : 0,
    "upsertedId" : 4
}
> db.restaurant.find()
{ "_id" : 1, "name" : "Central Perk Cafe", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Sub", "violations" : 5 }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "Borough" : "Manhattan", "violations" : 8 }

其他参数先不看吧,似乎很多是3.6!!

MongoDB CRUD之U

标签:.com   rip   col   block   删除   state   特定   com   document   

人气教程排行