MongoDB快速入門筆記(三)之MongoDB插入文檔操作
MongoDB 是一個基于分布式文件存儲的數(shù)據(jù)庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴展的高性能數(shù)據(jù)存儲解決方案。
MongoDB 是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。
本文給大家介紹MongoDB的插入文檔的方法,一起看看吧
1、文檔的數(shù)據(jù)存儲格式為BSON,類似于JSON。MongoDB插入數(shù)據(jù)時會檢驗數(shù)據(jù)中是否有“_id”,如果沒有會自動生成。
shell操作有insert和save兩種方法。當插入一條數(shù)據(jù)有“_id”值,并且現(xiàn)在集合中已經(jīng)有相同的值,使用insert插入時插入不進去,使用save時,會更新數(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)上的文檔都說不能MongoDB不支持批量插入,現(xiàn)在試過可以,應該是目前的版本支持批量插入了。
> 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快速入門筆記(三)之MongoDB插入文檔操作的相關(guān)知識,希望對大家有所幫助,更多精彩內(nèi)容,敬請關(guān)注腳本之家網(wǎng)站!
相關(guān)文章
Centos7安裝和卸載Mongodb數(shù)據(jù)庫的方法
MongoDB是一個跨平臺,面向文檔的數(shù)據(jù)庫,提供高性能,高可用性和易于擴展。MongoDB是工作在集合和文檔上一種概念。下面通過本文給大家分享Centos7安裝和卸載Mongodb數(shù)據(jù)庫的方法,需要的朋友參考下吧2017-11-11
Centos系統(tǒng)搭建MongoDB數(shù)據(jù)庫
這篇文章介紹了Centos系統(tǒng)搭建MongoDB數(shù)據(jù)庫的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
CentOS 7系統(tǒng)下SELinux阻止MongoDB啟動的問題詳解
開發(fā)分布式醫(yī)療掛號系統(tǒng)MongoDB集成實現(xiàn)上傳醫(yī)院接口
mongodb權(quán)限設(shè)置之添加管理員、普通用戶的方法

