MongoDB 插入文档

  • 插入一行数据

db.stus.insert({name:"zhubajie",age:18,gender:"male"});

  •  插入多行数据

db.stus.insert([
{name:"shaheshang",age:18,gender:"male"},
{name:"tangseng",age:18,gender:"male"}
]);

 当我们向集合中插入文档时,如果没有给文档指定_id属性,则数据库会自动添加_id,该属性用来作为文档的唯一标识。

  • 自己指定_id(不建议)

db.stus.insert({_id:"9527",name:"xiaobailong"});

  •  插入一个文档对象 db.collection.insertOne()

db.stus.insertOne({name:"baigujing",age:18,gender:"male"});

  • 插入多个文档对象 db.collection.insertMany()

db.stus.insertMany([
{name:"zhizhujing",age:18,gender:"male"},
{name:"yutujing",age:18,gender:"male"}
])

注意:insertOne()、insertMany() 和insert()是一样的功能。但insertOne()、insertMany() 语义更明确。在 3.2版本以上支持。

 

posted @   创客未来  阅读(107)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示