mongodb數(shù)據(jù)庫(kù)操作

查看有多少數(shù)據(jù)庫(kù)

> show dbs

admin0.000GB

local0.000GB

切換數(shù)據(jù)庫(kù)

>use admin

switchedtodb admin

在mongodb數(shù)據(jù)庫(kù)中不需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行創(chuàng)建,在需要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù)時(shí)mongodb會(huì)自動(dòng)創(chuàng)建

插入數(shù)據(jù),創(chuàng)建數(shù)據(jù)庫(kù)插入數(shù)據(jù)

db.imooc_collection.insert({x:1})

插入數(shù)據(jù)格式為json格式

> use imoocswitched to db imooc

> db.imooc_collection.insert({x:1})WriteResult({"nInserted":1})

> show dbsadmin0.000GBimooc0.000GBlocal0.000GB

> use imoocswitched to db imooc

> db.imooc_collection.insert({x:1})WriteResult({"nInserted":1})> show dbsadmin0.000GBimooc0.000GBlocal0.000GB

> use imoocswitched to db imooc

> db.dropDatabase(){"dropped":"imooc","ok":1}

> show dbsadmin0.000GBlocal0.000GB

查看創(chuàng)建的表

> show collectionsimooc_collection

查看插入的數(shù)據(jù)

_id : mongodb自動(dòng)生成的ID全局范圍內(nèi)不能重復(fù)

> db.imooc_collection.find()

{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}

> db.imooc_collection.insert({x:3,_id:1})

WriteResult({"nInserted":1})

> db.imooc_collection.insert({x:2,_id:1})

WriteResult({"nInserted":0,"writeError": {"code":11000,"errmsg":"E11000 duplicate key error collection: imooc.imooc_collection index: _id_ dup key: { : 1.0 }"}})

> db.imooc_collection.find()

{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}{"_id":1,"x":3}

> db.imooc_collection.find({x:1})

{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}

插入多條數(shù)據(jù)

>for(i=3;i<100;i++)db.imooc_collection.insert({x:i})

WriteResult({"nInserted":1})

> db.imooc_collection.find()

{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}{"_id":1,"x":3}{"_id": ObjectId("586cac66482469b789d37a77"),"x":3}{"_id": ObjectId("586cac66482469b789d37a78"),"x":4}{"_id": ObjectId("586cac66482469b789d37a79"),"x":5}{"_id": ObjectId("586cac66482469b789d37a7a"),"x":6}{"_id": ObjectId("586cac66482469b789d37a7b"),"x":7}{"_id":?

?> db.imooc_collection.find().count()

99

?> db.imooc_collection.find().skip(3).limit(2).sort({x:1}){"_id": ObjectId("586cac66482469b789d37a78"),"x":4}{"_id": ObjectId("586cac66482469b789d37a79"),"x":5}

數(shù)據(jù)的更新

> db.imooc_collection.find({x:1})

{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}

> db.imooc_collection.update({x:1},{x:999})

WriteResult({"nMatched":1,"nUpserted":0,"nModified":1})

> db.imooc_collection.find({x:1})

> db.imooc_collection.find({x:999})

{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":999}

更新部分字段

> db.imooc_collection.insert({x:100,y:100,z:100})

WriteResult({"nInserted":1})

> db.imooc_collection.update({z:100},{$set:{y:99}})

WriteResult({"nMatched":1,"nUpserted":0,"nModified":1})

> db.imooc_collection.find({z:100})

{"_id": ObjectId("586cae313728d003e066c223"),"x":100,"y":99,"z":100}

更新不存在的字段

> db.imooc_collection.find({y:100})

> db.imooc_collection.update({y:100},{y:999})

WriteResult({"nMatched":0,"nUpserted":0,"nModified":0})

> db.imooc_collection.find({y:999})

?> db.imooc_collection.update({y:100},{y:999},true)

WriteResult({"nMatched":0,"nUpserted":1,"nModified":0,"_id": ObjectId("586caf5f31b55e5759fd031d")})

> db.imooc_collection.find({y:999})

{"_id": ObjectId("586caf5f31b55e5759fd031d"),"y":999}

更新多條數(shù)據(jù)

> db.imooc_collection.find({c:2})

{"_id": ObjectId("586cafad3728d003e066c224"),"c":2}

> db.imooc_collection.find({c:1})

{"_id": ObjectId("586cafae3728d003e066c225"),"c":1}{"_id": ObjectId("586cafaf3728d003e066c226"),"c":1}

> db.imooc_collection.update({c:1},{$set:{c:2}},false,true)

WriteResult({"nMatched":2,"nUpserted":0,"nModified":2})

> db.imooc_collection.find({c:1})

> db.imooc_collection.find({c:2})

{"_id": ObjectId("586cafad3728d003e066c224"),"c":2}

{"_id": ObjectId("586cafae3728d003e066c225"),"c":2}

{"_id": ObjectId("586cafaf3728d003e066c226"),"c":2}

簡(jiǎn)書(shū)怎么使用markdown語(yǔ)法?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容