当前位置:网站首页>MongoDB-插入数据insert、insertOne、insertMany、save用法介绍
MongoDB-插入数据insert、insertOne、insertMany、save用法介绍
2022-07-18 21:33:00 【小博测试成长之路】
先针对上篇文章中MongoDB的_id字段含义做一些补充的描述,上篇文章地址:
补充说明:MongoDB是一个分布式的数据库,使用ObjectId类型数据作为主键的话,可以有效避免不同机器写入数据时_id的唯一性。上篇文章中有介绍过ObjectId类型的_id每一位对应的含义。
除了之前介绍过的,可以自定义_id的值以外,_id字段的类型除了可以用字符串、数字以外,还可以用一些比较复杂的数据格式做主键:
db.getCollection("user").insert({"_id":{"regionId":6,"name":"小燕子"}, "name": "小燕子","age": 18,"height": 178})
接下来我们一起看一下在mongodb中要插入数据的话,格式是怎么样的:
插入单条数据语法:
db.collection.insert( <document>, {writeConcern: <document>, ordered: <boolean> })db.collection.insertOne( <document>, {writeConcern: <document> })
使用案例:
db.getCollection("user").insert({"name": "尔康"})
db.getCollection("user").insert({"name": "尔康"},{ordered:true})
db.getCollection("user").insertOne({name: "金锁", "age": 45, "sex": "girl"})
db.getCollection("user").insertOne({_id: "No.01",name: "尔泰", "age": 36, "sex": "boy"})
注意事项:
insertOne函数一次只能插入一条数据,如果插入多条的话,是会报错的:
插入多条:
db.collection.insert([ <document 1> , <document 2>, ... ], {writeConcern: <document>, ordered: <boolean> })db.collection.insertMany([ <document 1> , <document 2>, ... ], {writeConcern: <document>, ordered: <boolean> })
参数说明(部分参数在单条和多条插入时均可用):
document:要插入的数据,一般为bson格式,看上去就跟json差不多,或者python里面的dict
writeConcern(可选):写入策略,一般可以不写,有个简单的了解即可。
具体用法参考官方文档:https://www.mongodb.com/docs/manual/reference/write-concern/
csdn上也有其他人对其进行整理中文的描述:https://blog.csdn.net/xiaoliuliu2050/article/details/102951111
ordered(可选):指定是否按顺序写入,默认 true,按顺序写入。为true时,插入多条数据时,有一条报错就中断后面的操作。为false,报错的被跳过,后面的继续执行。
使用案例:
db.getCollection("user").insert([
{ _id: "No.01", name: "紫薇", "age": 50, "sex": "girl" },
{ name: "乾隆", "age": 70, "sex": "boy" },
{ name: "五阿哥", "age": 45, "sex": "boy" }
])
db.getCollection("user").insertMany([
{ _id: "No.02", name: "萧十一郎", "age": 50, "sex": "girl" },
{ name: "杨开泰", "age": 70, "sex": "boy" },
{ name: "连城璧", "age": 45, "sex": "boy" }
])
save方法:
db.getCollection("user").save({_id: "No.01",name: "尔泰", "age": 36, "sex": "boy"})
db.getCollection("user").save({_id: "No.01",name: "尔泰1", "age": 36, "sex": "boy"})
db.user.find({})
从上图可以看出:save是插入/覆盖操作。
注意:save方法一次只能操作一条记录,操作多条的时候会报错:
insertOne和save不同,主键冲突时insertOne会报错,而save会直接用新值覆盖久值
如果文章对你有帮助,记得转发、留言、点赞、加关注哦!
如果你对目前的工作比较困惑,不知道如何去提升自己,欢迎报名咨询付费服务,详细介绍可见:职场大咖带你助攻面试求职+职业发展
END
所有原创文章
第一时间发布至此公众号「小博测试成长之路」
长按二维码/微信扫码 添加作者
边栏推荐
- 图像处理 OpenCV简介
- 如何提取差异脑区的灰质体积与临床量表算相关?——基于体素的形态学方法(VBM)
- ICML2022 | 通过评估演示者的专业知识进行模仿学习
- [target detection] yolov5: model construction and analysis
- Introduction to image processing opencv
- y70.第四章 Prometheus大厂监控体系及实战 -- Prometheus监控介绍(一)
- Google 版 emoji 猜词游戏,助家长引导孩子安全上网
- 使用字段参数进行报表翻译
- Learning exploration - rollover image rotation stacking effect
- Mhchxm ultrafast recovery diode sff1004 parameters, sff1004 characteristics
猜你喜欢
Scientific and technological achievements to promote maker Engineering Education
还在羡慕其它平台有跨店满减,其实你也可以!
[target detection] yolov5 improved model for small target detection / add frame rate detection
使用字段参数进行报表翻译
数学建模:绘图经验总结
Movinets series models are good helpers for real-time classified videos on mobile phones
Several small open source projects of mine over the years
基于sql net客户服务关系管理企业信使企信通
C language: high-order usage of scanf()
【目标检测】YOLOv5针对小目标检测的改进模型/添加帧率检测
随机推荐
挖财开的证券账户安全吗?在哪里可以开证券账户
【目标检测】YOLOv5:模型构建解析
clock_gettime
When the transfer strict selection order encounters the state machine
Implement the basic responsibilities of steam education in primary and secondary schools
交换机与路由器技术:动态路由协议、RIP路由协议和OSPF路由协议
How much is the Commission for opening an account of Huatai Securities? Is it safe to open an account online
【企微vconsole】
SG90舵机驱动,有代码
查缺补漏C语言:字符串
解析机器人视觉系统的神奇之处
C language guessing numbers game
Some JS data processing methods in daily development, including data filtering and data verification
What is pricat message?
bsdiff和bspatch增量更新
Applet_ Make button
[binary tree] sum of nodes with even value of grandfather nodes
小程序_造按钮
9款最佳项目集管理工具
StarRocks理论介绍