MongoDB使用Bulk批量更新操作 对于mongodb中更新操作,如何采用“批量更新方式”
yuyutoo 2024-10-28 20:21 2 浏览 0 评论
概述
MongoDB支持执行批量更新和插入操作,允许在一次操作中插入或检索多个文档。通过使用Batch接口,减少客户端和数据库之间的调用次数,可以显著提高数据库访问性能。
本文将演示如何使用MongoDB Shell和Java代码两种方式实现文档的批量更新。
数据库初始化
首先,我们需要连接到mongo shell:
mongo --host localhost --port 27017
建立一个数据库testDb和一个populations集合:
use testDb;
db.createCollection(populations);
使用insertMany方法将一些样本数据添加到集合中:
db.populations.insertMany([
{
"cityId":1124,
"cityName":"New York",
"countryName":"United States",
"continentName":"North America",
"population":22
},
{
"cityId":1125,
"cityName":"Mexico City",
"countryName":"Mexico",
"continentName":"North America",
"population":25
},
{
"cityId":1126,
"cityName":"New Delhi",
"countryName":"India",
"continentName":"Asia",
"population":45
},
{
"cityId":1134,
"cityName":"London",
"countryName":"England",
"continentName":"Europe",
"population":32
}]);
执行上述insertMany查询将返回以下文档:
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("623575049d55d4e137e477f6"),
ObjectId("623575049d55d4e137e477f7"),
ObjectId("623575049d55d4e137e477f8"),
ObjectId("623575049d55d4e137e477f9")
]
}
使用MongoDB Shell查询
MongoDB的批量操作生成器用于为单个集合批量构建写操作列表,可以使用insert、update、replace和remove等方法执行不同类型的操作:
db.populations.bulkWrite([
{
insertOne :
{
"document" :
{
"cityId":1128,
"cityName":"Kathmandu",
"countryName":"Nepal",
"continentName":"Asia",
"population":12
}
}
},
{
insertOne :
{
"document" :
{
"cityId":1130,
"cityName":"Mumbai",
"countryName":"India",
"continentName":"Asia",
"population":55
}
}
},
{
updateOne :
{
"filter" :
{
"cityName": "New Delhi"
},
"update" :
{
$set :
{
"status" : "High Population"
}
}
}
},
{
updateMany :
{
"filter" :
{
"cityName": "London"
},
"update" :
{
$set :
{
"status" : "Low Population"
}
}
}
},
{
deleteOne :
{
"filter" :
{
"cityName":"Mexico City"
}
}
},
{
replaceOne :
{
"filter" :
{
"cityName":"New York"
},
"replacement" :
{
"cityId":1124,
"cityName":"New York",
"countryName":"United States",
"continentName":"North America",
"population":28
}
}
}
]);
上述bulkWrite查询将返回以下文档:
{
"acknowledged" : true,
"deletedCount" : 1,
"insertedCount" : 2,
"matchedCount" : 3,
"upsertedCount" : 0,
"insertedIds" :
{
"0" : ObjectId("623575f89d55d4e137e477f9"),
"1" : ObjectId("623575f89d55d4e137e477fa")
},
"upsertedIds" : {}
}
使用Java进行批量操作
首先创建一个MongoClient连接:
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("testDb");
MongoCollection<Document> collection = database.getCollection("populations");
使用Java代码实现相同的批量操作:
List<WriteModel<Document>> writeOperations = new ArrayList<WriteModel<Document>>();
writeOperations.add(new InsertOneModel<Document>(new Document("cityId", 1128)
.append("cityName", "Kathmandu")
.append("countryName", "Nepal")
.append("continentName", "Asia")
.append("population", 12)));
writeOperations.add(new InsertOneModel<Document>(new Document("cityId", 1130)
.append("cityName", "Mumbai")
.append("countryName", "India")
.append("continentName", "Asia")
.append("population", 55)));
writeOperations.add(new UpdateOneModel<Document>(new Document("cityName", "New Delhi"),
new Document("$set", new Document("status", "High Population"))
));
writeOperations.add(new UpdateManyModel<Document>(new Document("cityName", "London"),
new Document("$set", new Document("status", "Low Population"))
));
writeOperations.add(new DeleteOneModel<Document>(new Document("cityName", "Mexico City")));
writeOperations.add(new ReplaceOneModel<Document>(new Document("cityId", 1124),
new Document("cityName", "New York").append("cityName", "United States")
.append("continentName", "North America")
.append("population", 28)));
BulkWriteResult bulkWriteResult = collection.bulkWrite(writeOperations);
System.out.println("bulkWriteResult:- " + bulkWriteResult);
首先创建了一个writeModel列表,将所有不同类型的写操作添加到一个更新列表中。此外,我们在查询中使用了InsertOneModel、UpdateOneModel、UpdateManyModel、DeleteOneModel和ReplaceOneModel。最后,bulkWrite方法一次执行了所有的操作。
结论
MongoDB的Bulk批量操作详细资料可以参考:https://www.mongodb.com/docs/manual/core/bulk-write-operations/
相关推荐
- 如何在HTML中使用JavaScript:从基础到高级的全面指南!
-
“这里是云端源想IT,帮你...
- 推荐9个Github上热门的CSS开源框架
-
大家好,我是Echa。...
- 硬核!知网首篇被引过万的论文讲了啥?作者什么来头?
-
整理|袁小华近日,知网首篇被引量破万的中文论文及其作者备受关注。知网中心网站数据显示,截至2021年7月23日,由华南师范大学教授温忠麟等人发表在《心理学报》2004年05期上的学术论文“中介效应检验...
- 为什么我推荐使用JSX开发Vue3_为什么用vue不用jquery
-
在很长的一段时间中,Vue官方都以简单上手作为其推广的重点。这确实给Vue带来了非常大的用户量,尤其是最追求需求开发效率,往往不那么在意工程代码质量的国内中小企业中,Vue占据的份额极速增长...
-
- 【干货】一文详解html和css,前端开发需要哪些技术?
-
网站开发简介...
-
2025-02-20 18:34 yuyutoo
- 分享几个css实用技巧_cssli
-
本篇将介绍几个css小技巧,目录如下:自定义引用标签的符号重置所有标签样式...
- 如何在浏览器中运行 .NET_怎么用浏览器运行代码
-
概述:...
- 前端-干货分享:更牛逼的CSS管理方法-层(CSS Layers)
-
使用CSS最困难的部分之一是处理CSS的权重值,它可以决定到底哪条规则会最终被应用,尤其是如果你想在Bootstrap这样的框架中覆盖其已有样式,更加显得麻烦。不过随着CSS层的引入,这一...
-
- HTML 基础标签库_html标签基本结构
-
HTML标题HTML标题(Heading)是通过-...
-
2025-02-20 18:34 yuyutoo
- 前端css面试20道常见考题_高级前端css面试题
-
1.请解释一下CSS3的flexbox(弹性盒布局模型),以及适用场景?display:flex;在父元素设置,子元素受弹性盒影响,默认排成一行,如果超出一行,按比例压缩flex:1;子元素设置...
- vue引入外部js文件并使用_vue3 引入外部js
-
要在Vue中引入外部的JavaScript文件,可以使用以下几种方法:1.使用``标签引入外部的JavaScript文件。在Vue的HTML模板中,可以直接使用``标签来引入外部的JavaScrip...
- 网页设计得懂css的规范_html+css网页设计
-
在初级的前端工作人员,刚入职的时候,可能在学习前端技术,写代码不是否那么的规范,而在工作中,命名的规范的尤为重要,它直接与你的代码质量挂钩。网上也受很多,但比较杂乱,在加上每年的命名都会发生一变化。...
- Google在Chrome中引入HTML 5.1标记
-
虽然负责制定Web标准的WorldWideWebConsortium(W3C)尚未宣布HTML5正式推荐规格,而Google已经迁移到了HTML5.1。即将发布的Chrome38将引入H...
- HTML DOM 引用( ) 对象_html中如何引用js
-
引用对象引用对象定义了一个同内联元素的HTML引用。标签定义短的引用。元素经常在引用的内容周围添加引号。HTML文档中的每一个标签,都会创建一个引用对象。...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)