首页 / MONGODB / MongoDB CRUD之U
MongoDB CRUD之U
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MongoDB CRUD之U,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6889字,纯文字阅读大概需要10分钟。
内容图文
![MongoDB CRUD之U](/upload/InfoBanner/zyjiaocheng/500/f32b16e7dd844ddbb3eded8d86af2786.jpg)
加载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
本文系统来源:https://www.cnblogs.com/oneTOinf/p/8451396.html
内容总结
以上是互联网集市为您收集整理的MongoDB CRUD之U全部内容,希望文章能够帮你解决MongoDB CRUD之U所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。