這期內(nèi)容當中小編將會給大家?guī)碛嘘PMongoDB中怎么實現(xiàn)數(shù)據(jù)增加操作,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)是一家從事企業(yè)網(wǎng)站建設、成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設、行業(yè)門戶網(wǎng)站建設、網(wǎng)頁設計制作的專業(yè)的建站公司,擁有經(jīng)驗豐富的網(wǎng)站建設工程師和網(wǎng)頁設計人員,具備各種規(guī)模與類型網(wǎng)站建設的實力,在網(wǎng)站建設領域樹立了自己獨特的設計風格。自公司成立以來曾獨立設計制作的站點上1000家。
只要是數(shù)據(jù)庫那么就絕對離不開最為核心的功能:CRUD,所以在MongoDB里面對于數(shù)據(jù)的操作也是支持的,但是需要提醒的是,除了增加之外,其它的都很麻煩。
1、數(shù)據(jù)增加
使用“db.集合.insert()”可以實現(xiàn)數(shù)據(jù)的增加操作。
范例:增加一個簡單數(shù)據(jù)
> use hr
switched to db hr
> db.info.insert({"url":"www.stone.com"});
WriteResult({ "nInserted" : 1 })
> db.info.find();
{ "_id" : ObjectId("5990f8ea3268c8e84253ba3b"), "url" : "www.stone.com" }
范例:保存數(shù)組
> db.info.insert([{"url":"www.stone.com"},{"url":"www.stonedb.com"}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
> db.info.find();
{ "_id" : ObjectId("5990f8ea3268c8e84253ba3b"), "url" : "www.stone.com" }
{ "_id" : ObjectId("5990f97d3268c8e84253ba3c"), "url" : "www.stone.com" }
{ "_id" : ObjectId("5990f97d3268c8e84253ba3d"), "url" : "www.stonedb.com" }
如果想要保存多個數(shù)據(jù),那么就使用數(shù)組。
范例:保存1000個數(shù)據(jù)
> for(var x=0;x<1000;x++){db.info.insert({"url":"stone_"+x});}
WriteResult({ "nInserted" : 1 })
> db.info.find();
{ "_id" : ObjectId("5990f8ea3268c8e84253ba3b"), "url" : "www.stone.com" }
{ "_id" : ObjectId("5990f97d3268c8e84253ba3c"), "url" : "www.stone.com" }
{ "_id" : ObjectId("5990f97d3268c8e84253ba3d"), "url" : "www.stonedb.com" }
{ "_id" : ObjectId("5990faea3268c8e84253ba3e"), "url" : "stone_0" }
{ "_id" : ObjectId("5990faea3268c8e84253ba3f"), "url" : "stone_1" }
{ "_id" : ObjectId("5990faea3268c8e84253ba40"), "url" : "stone_2" }
{ "_id" : ObjectId("5990faea3268c8e84253ba41"), "url" : "stone_3" }
{ "_id" : ObjectId("5990faea3268c8e84253ba42"), "url" : "stone_4" }
{ "_id" : ObjectId("5990faea3268c8e84253ba43"), "url" : "stone_5" }
{ "_id" : ObjectId("5990faea3268c8e84253ba44"), "url" : "stone_6" }
{ "_id" : ObjectId("5990faea3268c8e84253ba45"), "url" : "stone_7" }
{ "_id" : ObjectId("5990faea3268c8e84253ba46"), "url" : "stone_8" }
{ "_id" : ObjectId("5990faea3268c8e84253ba47"), "url" : "stone_9" }
{ "_id" : ObjectId("5990faea3268c8e84253ba48"), "url" : "stone_10" }
{ "_id" : ObjectId("5990faea3268c8e84253ba49"), "url" : "stone_11" }
{ "_id" : ObjectId("5990faea3268c8e84253ba4a"), "url" : "stone_12" }
{ "_id" : ObjectId("5990faea3268c8e84253ba4b"), "url" : "stone_13" }
{ "_id" : ObjectId("5990faea3268c8e84253ba4c"), "url" : "stone_14" }
{ "_id" : ObjectId("5990faea3268c8e84253ba4d"), "url" : "stone_15" }
{ "_id" : ObjectId("5990faea3268c8e84253ba4e"), "url" : "stone_16" }
Type "it" for more
> it
{ "_id" : ObjectId("5990faea3268c8e84253ba4f"), "url" : "stone_17" }
{ "_id" : ObjectId("5990faea3268c8e84253ba50"), "url" : "stone_18" }
上述就是小編為大家分享的MongoDB中怎么實現(xiàn)數(shù)據(jù)增加操作了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。