MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
本文给大家介绍MongoDB的插入文档的方法,一起看看吧
1、文档的数据存储格式为BSON,类似于JSON。MongoDB插入数据时会检验数据中是否有“_id”,如果没有会自动生成。
shell操作有insert和save两种方法。当插入一条数据有“_id”值,并且现在集合中已经有相同的值,使用insert插入时插入不进去,使用save时,会更新数据。
> db.student.drop() true > db.student.insert({"_id": 1, "name":"zhangsan", "age": 28}) WriteResult({ "nInserted" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 28 } > db.student.insert({"_id": 1, "name":"zhangsan", "age": 27}) WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: zyhdb.student index: _id_ dup key: { : 1.0 }" } }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 28 } > db.student.save({"_id": 1, "name":"zhangsan", "age": 27}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 27 }
2、批量插入,网上的文档都说不能MongoDB不支持批量插入,现在试过可以,应该是目前的版本支持批量插入了。
> db.student.insert([{"_id": 2, "name": "lisi"},{"_id": 3, "name": "wangwu"}, {"_id": 4, "name": "zhaoliu", "age": 28}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 27 } { "_id" : , "name" : "lisi" } { "_id" : , "name" : "wangwu" } { "_id" : 4, "name" : "zhaoliu", "age" : 28 }
3、循环插入:
> for(var i=; i<; i++){db.fortest.insert({num: i})} WriteResult({ "nInserted" : }) > db.fortest.find() { "_id" : ObjectId("eceadaeabab"), "num" : 0} { "_id" : ObjectId("eceadaeabab"), "num" : 1} { "_id" : ObjectId("eceadaeabab"), "num" : 2} { "_id" : ObjectId("eceadaeabab"), "num" : 3} { "_id" : ObjectId("eceadaeabab"), "num" : 4} { "_id" : ObjectId("eceadaeababa"), "num" : 5} { "_id" : ObjectId("eceadaeababb"), "num" : 6} { "_id" : ObjectId("eceadaeababc"), "num" : 7} { "_id" : ObjectId("eceadaeababd"), "num" : 8} { "_id" : ObjectId("eceadaeababe"), "num" : 9}
以上所述是小编给大家介绍的MongoDB快速入门笔记(三)之MongoDB插入文档操作的相关知识,希望对大家有所帮助,更多精彩内容,敬请关注网站!
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年09月19日
2024年09月19日
- 【原神】V5.0攻略 |「雷电将军」一图流丨角色解析攻略
- 《原神》4.7被埋藏的荣耀任务怎么做?
- 尤雅.1995-情海茫茫【怀登唱片】【WAV+CUE】
- 张宇.1995-一言难揪EMI百代】【WAV+CUE】
- 尹毓恪.2018-浴室与太平洋【海蝶】【FLAC分轨】
- 《遗迹2:黑暗的视界》发布全新职业护卫者预告片,将于9月25日上线
- 好评如潮《彼方的她》迎来首次大型更新
- 《最终幻想16》PC版震撼登场 官方发布精彩宣传片
- 《中国十大国语老歌.男人篇 3CD》[WAV/分轨][1.5GB]
- 《汽车音响极品流行热曲发烧大碟 情伤 男声篇2CD》[WAV/分轨][980MB]
- 《绝版经典情歌 国语老歌 内地珍藏篇 2CD》[WAV/分轨][1.2GB]
- 崩坏星穹铁道冬城史学家成就获得攻略
- 金铲铲之战符文大陆城邦效果分享 有哪些效果
- 仙境传说新启航单手斧流派技能流铁匠玩法介绍
- 归龙潮笼鸟意未平任务怎么完成 笼鸟意未平任务流程攻略