MongoDB快速入門(mén)筆記(三)之MongoDB插入文檔操作
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由 C++ 語(yǔ)言編寫(xiě)。旨在為 WEB 應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。
本文給大家介紹MongoDB的插入文檔的方法,一起看看吧
1、文檔的數(shù)據(jù)存儲(chǔ)格式為BSON,類(lèi)似于JSON。MongoDB插入數(shù)據(jù)時(shí)會(huì)檢驗(yàn)數(shù)據(jù)中是否有“_id”,如果沒(méi)有會(huì)自動(dòng)生成。
shell操作有insert和save兩種方法。當(dāng)插入一條數(shù)據(jù)有“_id”值,并且現(xiàn)在集合中已經(jīng)有相同的值,使用insert插入時(shí)插入不進(jìn)去,使用save時(shí),會(huì)更新數(shù)據(jù)。
> 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、批量插入,網(wǎng)上的文檔都說(shuō)不能MongoDB不支持批量插入,現(xiàn)在試過(guò)可以,應(yīng)該是目前的版本支持批量插入了。
> 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、循環(huán)插入:
> 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快速入門(mén)筆記(三)之MongoDB插入文檔操作的相關(guān)知識(shí),希望對(duì)大家有所幫助,更多精彩內(nèi)容,敬請(qǐng)關(guān)注腳本之家網(wǎng)站!
相關(guān)文章
MongoDB 內(nèi)存管理相關(guān)總結(jié)
這篇文章主要介紹了MongoDB 內(nèi)存管理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用MongoDB數(shù)據(jù)庫(kù),感興趣的朋友可以了解下2021-03-03MongoDB數(shù)據(jù)庫(kù)的特色和優(yōu)點(diǎn)介紹
這篇文章主要介紹了MongoDB數(shù)據(jù)庫(kù)的特色和優(yōu)點(diǎn)介紹,本文總結(jié)了每個(gè)開(kāi)發(fā)人員都應(yīng)該知道的5個(gè)MongoDB特點(diǎn),需要的朋友可以參考下2015-05-05MongoDB磁盤(pán)IO問(wèn)題的3種解決方法
磁盤(pán)IO是不可避免的,除去減少或延緩磁盤(pán)操作,也需要盡量的增強(qiáng)磁盤(pán)IO性能和吞吐量。下面這篇文章主要給大家介紹了關(guān)于MongoDB磁盤(pán)IO問(wèn)題的3種解決方法,需要的朋友可以參考借鑒,需要的朋友們下面來(lái)一起看看吧2018-07-07mongodb使用docker搭建replicaSet集群與變更監(jiān)聽(tīng)(最新推薦)
replicaSet和cluster從部署難度相比,replicaSet要簡(jiǎn)單許多。如果所存儲(chǔ)的數(shù)據(jù)量規(guī)模不算太大的情況下,那么使用replicaSet方式部署mongodb是一個(gè)不錯(cuò)的選擇,這篇文章主要介紹了mongodb使用docker搭建replicaSet集群與變更監(jiān)聽(tīng),需要的朋友可以參考下2023-03-03Mongodb中MapReduce實(shí)現(xiàn)數(shù)據(jù)聚合方法詳解
Mongodb是針對(duì)大數(shù)據(jù)量環(huán)境下誕生的用于保存大數(shù)據(jù)量的非關(guān)系型數(shù)據(jù)庫(kù),針對(duì)大量的數(shù)據(jù)。接下來(lái)通過(guò)本文給大家介紹Mongodb中MapReduce實(shí)現(xiàn)數(shù)據(jù)聚合方法詳解,感興趣的朋友一起學(xué)習(xí)吧2016-05-05Ubuntu系統(tǒng)中安裝MongoDB及其啟動(dòng)命令mongod的教程
這篇文章主要介紹了Ubuntu系統(tǒng)中安裝MongoDB及其啟動(dòng)命令mongod的教程,包括設(shè)置MongoDB開(kāi)機(jī)啟動(dòng)的腳本示例,非常推薦,需要的朋友可以參考下2016-01-01