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版本以上支持。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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框架的用法!