您好,登錄后才能下訂單哦!
這篇文章主要介紹“MongoDB數(shù)據(jù)庫常用的操作命令有哪些”,在日常操作中,相信很多人在MongoDB數(shù)據(jù)庫常用的操作命令有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”MongoDB數(shù)據(jù)庫常用的操作命令有哪些”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
1. 顯示全部可用數(shù)據(jù)庫
2. 切換數(shù)據(jù)庫
3. 顯示數(shù)據(jù)集
> show dbs;
該命令將展示 mongo 的全部數(shù)據(jù)庫名稱,并列出來。
> use mydb;
該命令會(huì)選擇一個(gè)指定的數(shù)據(jù)庫,如果數(shù)據(jù)庫不存在,則會(huì)自動(dòng)創(chuàng)建一個(gè)。但是需要注意,由于此時(shí)數(shù)據(jù)庫沒有數(shù)據(jù),因此當(dāng)使用 show dbs命令的時(shí)候,看不到該數(shù)據(jù)庫。只有插入了數(shù)據(jù)集后才可以看到。
> show collections;
插入數(shù)據(jù)的格式為 db.{數(shù)據(jù)集名}.insert({數(shù)據(jù)鍵值對(duì)}),成功后返回插入的條數(shù)。
> db.test.insert({"name": "島上碼農(nóng)"}); WriteResult({ "nInserted" : 1 })
插入多條數(shù)據(jù)使用中括號(hào)括起來即可,此時(shí)返回的是批量操作結(jié)果,其中 nInserted 返回的是成功插入的條數(shù)。。
> db.test.insert([{"name": "島上碼農(nóng)"},{"name": "掘金"}]); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
更新一條數(shù)據(jù)的命令如下,其中格式為 db.{數(shù)據(jù)集名}.update({查詢條件}, {$set: {更新后數(shù)據(jù)}})。
> db.test.update({"name": "島上碼農(nóng)"}, {$set: {"name": "碼農(nóng)"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
以上命令只會(huì)更新一條匹配的數(shù)據(jù),如果要更新多條,需要增加參數(shù):{multi: true}。
> db.test.update({"name": "島上碼農(nóng)"}, {$set: {"name": "碼農(nóng)"}}, {multi: true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
也可以使用 updateMany 更新多條。
> db.test.updateMany({"name": "碼農(nóng)"}, {$set: {"name": "島上碼農(nóng)"}}); { "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
替換文檔會(huì)使用新的文檔替換掉已有的文檔,其中格式為 db.{數(shù)據(jù)集名}.save({新文檔數(shù)據(jù)})。例如下面的例子替換了_id 為60c8a50adb9890bf41255fe4的文檔。
> db.test.save({"_id": "60c8a50adb9890bf41255fe4", "name": "島上碼農(nóng)-1"}); WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "60c8a50adb9890bf41255fe4" })
查詢數(shù)據(jù)命令為格式為 db.{數(shù)據(jù)集名}.find()。如果需要限制條數(shù)可以加limit(n)。
> db.test.find();
查詢出來的格式需要美化的話,加上 pretty()即可。
> db.test.find().pretty();
按條件查詢時(shí),在 find 中添加篩選參數(shù)即可。
> db.test.find({"name":"島上碼農(nóng)"}).pretty();
統(tǒng)計(jì)時(shí)使用 count()函數(shù)即可,如果需要篩選也是在 find 方法中傳篩選條件即可。
> db.test.find().count();
刪除文檔的格式為db.test.remove({篩選條件});
> db.test.remove({"name":"島上碼農(nóng)-1"}); WriteResult({ "nRemoved" : 1 })
刪除一條的使用 deleteOne 方法,刪除多條使用 deleteMany 方法。
> db.test.deleteOne({"name":"島上碼農(nóng)"}); { "acknowledged" : true, "deletedCount" : 1 } > db.test.deleteMany({"name":"島上碼農(nóng)"}); { "acknowledged" : true, "deletedCount" : 2 }
對(duì)于有些命令不懂操作的,查看操作文檔即可,命令格式為 db.{數(shù)據(jù)集名}.help()。
到此,關(guān)于“MongoDB數(shù)據(jù)庫常用的操作命令有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。