mongodb shell基础命令
mongodb shell命令
1.數(shù)據(jù)庫(kù)基本操作
在mongodb中,使用use來(lái)創(chuàng)建和選擇數(shù)據(jù)庫(kù),當(dāng)數(shù)據(jù)庫(kù)不存在時(shí),use會(huì)創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù),但是這數(shù)據(jù)庫(kù)并沒(méi)有持久化到硬盤里面,而存在內(nèi)存中,只有當(dāng)用戶往這個(gè)新創(chuàng)建的空數(shù)據(jù)庫(kù)建collection才會(huì)做持久化操作。
創(chuàng)建和切換數(shù)據(jù)庫(kù)
use 數(shù)據(jù)庫(kù)名稱查看有權(quán)限查看的所有的數(shù)據(jù)庫(kù)
mongodb是不對(duì)空數(shù)據(jù)庫(kù)做持久化的,所以新創(chuàng)建的空數(shù)據(jù)庫(kù),用show指令是看不見(jiàn)的,必須建collection后才能看見(jiàn)。
show dbs show databases查看當(dāng)前正在使用的數(shù)據(jù)庫(kù)
在mongo中db就代表了你當(dāng)前正在使用的數(shù)據(jù)庫(kù)對(duì)象。
db刪除數(shù)據(jù)庫(kù)
db代表當(dāng)前正在使用的數(shù)據(jù)庫(kù)對(duì)象,刪除數(shù)據(jù)庫(kù)前,先要切換到要?jiǎng)h除的數(shù)據(jù)庫(kù)。主要用來(lái)刪除已經(jīng)持久化的數(shù)據(jù)庫(kù)。
db.dropDatabase()查看數(shù)據(jù)庫(kù)用戶
show users2.集合的基本操作
在mongodb中集合(collection)對(duì)應(yīng)mysql中的表(table)
顯式創(chuàng)建collection
db.createCollection("CollectionName")查看collection
可以看到mongodb一些操作命令是跟mysql兼容的。
show collections show tables隱式創(chuàng)建collection
當(dāng)我們向一個(gè)不存在的collection插入一個(gè)文檔(document)時(shí),會(huì)先隱式創(chuàng)建一個(gè)collection,再進(jìn)行數(shù)據(jù)的插入。
db.myCollection.insertOne( { name: "xiaoming" } );3.增刪改查
在mongodb中每一行數(shù)據(jù)對(duì)應(yīng)一個(gè)文檔(document),每個(gè)文檔是一種類似于 JSON 的 格式叫BSON,所以它既可以存儲(chǔ)比較復(fù)雜的數(shù)據(jù)類型,又相當(dāng)?shù)撵`活。BSON可以是二進(jìn)制形式的JSON。MongoDB中的記錄是一個(gè)文檔,它是一個(gè)由字段和值對(duì)(fifield:value)組成的數(shù)據(jù)結(jié)構(gòu)。MongoDB文檔類似于JSON對(duì)象,即一個(gè)文檔認(rèn)為就是一個(gè)對(duì)象。字段的數(shù)據(jù)類型是字符型,它的值除了使用基本的一些類型外,還可以包括其他文檔、普通數(shù)組和文檔數(shù)組。
3.1 增
插入一個(gè)文檔
insertOne是用來(lái)插入一個(gè)文檔的,當(dāng)傳入多個(gè)文檔文檔對(duì)象時(shí),就會(huì)報(bào)錯(cuò)。
用法:
db.collectionName.insertOne(<document>, { writeConcern:<document>} )- writeConcern:寫關(guān)注,可選。
插入多個(gè)文檔
insertMany用來(lái)插入多個(gè)文檔(document),傳入的是一個(gè)數(shù)組對(duì)象,里面包含了多個(gè)插入對(duì)象。
用法:
db.collectionName.insertOne( [<document1>,<document2>], { writeConcern:<document>,ordered:boolean} )- writeConcern:寫關(guān)注,可選。
- ordered:執(zhí)行有序插入還是無(wú)序插入。默認(rèn)為true。
列子:
db.inventory.insertMany([{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } } ])插入一個(gè)或多個(gè)
insert方法可以插入一個(gè)document也可以插入多個(gè)document,這里就不多說(shuō)了,當(dāng)成insertMany+insertOne綜合來(lái)用就可以了。
用法:
db.inventory.insert({[<document1>,<document2>]}, { writeConcern:<document>,ordered:boolean} )3.2 查
查詢多條信息
查使用find()方法
用法:
db.collectionName.find(query,projection)- query:查詢條件
- projection:指定包含的字段
例子:
#查詢包含size: { h: 14, w: 21, uom: "cm" }的文檔 db.collection.find( { size: { h: 14, w: 21, uom: "cm" } } ) #傳入一個(gè){name:1}來(lái)表示指定顯示name字段,其他字段不顯示 db.collection.find( { size: { h: 14, w: 21, uom: "cm" } },{name:1} )查詢一條信息
查詢一條信息用findOne()方法,調(diào)用此方法,返回的是查詢到的第一條信息,當(dāng)找到第一條信息時(shí)就不會(huì)繼續(xù)往下找了。
用法:
db.collectionName.findOne(query,projection)例子:
#查詢size: { h: 14, w: 21, uom: "cm" }的文檔,找到立即返回 db.collection.find( { size: { h: 14, w: 21, uom: "cm" } } )分頁(yè)查詢
limit(num),指定要查詢的條數(shù)。
skip(num),指定跳過(guò)的條數(shù)
//查詢前面前前三條數(shù)據(jù) db.collection.find().limit(3) //從第2條開(kāi)始查詢,查到第5條 db.collection.find().skip(2).limit(3)排序查詢
sort() 方法對(duì)數(shù)據(jù)進(jìn)行排序,sort() 方法可以通過(guò)參數(shù)指定排序的字段,并使用 1 和 -1 來(lái)指定排序的方式,其中 1 為升序排列,而 -1 是用于降序排列。
//按userid的降序 db.collection.find().sort({userid:-1}) //按userid的升序 db.collection.find().sort({userid:1})正則查詢
mongodb支持正則表達(dá)式,放在 / 之間
db.collect.find({userid:/正則表達(dá)式子/})注意:skip(), limilt(), sort()三個(gè)放在一起執(zhí)行的時(shí)候,執(zhí)行的順序是先 sort(), 然后是 skip(),最后是顯示的 limit()。
條件查詢
查詢時(shí)我們往往需要一些比較條件,>,<,or,and等等。
db.集合名稱.find({ "field" : { $gt: value }}) // 大于: field > value db.集合名稱.find({ "field" : { $lt: value }}) // 小于: field < value db.集合名稱.find({ "field" : { $gte: value }}) // 大于等于: field >= value db.集合名稱.find({ "field" : { $lte: value }}) // 小于等于: field <= value db.集合名稱.find({ "field" : { $ne: value }}) // 不等于: field != value- $gt 大于
- $lt 小于
- $gte 不大于
- $lte 不小于
- $ne 不等于
- $in 包含
- $nin 不包含
3.3改
使用update()方法可以進(jìn)行collection的文檔信息的更改。
用法:
db.collection.update(query, update, options) db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document>, collation: <document>, arrayFilters: [ <filterdocument1>, ... ], hint: <document|string> // Available starting in MongoDB 4.2 } )- query:查詢條件
- update:更改的信息(默認(rèn)會(huì)直接覆蓋整個(gè)文檔)
- upsert:可選。如果設(shè)置為true,則在沒(méi)有與查詢條件匹配的文檔時(shí)創(chuàng)建新文檔。默認(rèn)值為false,如果找不到匹配項(xiàng),則不會(huì)插入新文檔。
- multi :可選。如果設(shè)置為true,則更新符合查詢條件的多個(gè)文檔。如果設(shè)置為false,則更新一個(gè)文檔。默認(rèn)值為false。
例子:
//使用$set符可以只修改文檔中的likenum字段,而非直接將整個(gè)文檔覆蓋 db.collection.update({_id:"2"},{$set:{likenum:NumberInt(889)}}) //查詢所有包含userid=1003的信息,并修改其中的likenum字段,multi為更新符合查詢條件的多個(gè)文檔 db.collection.update({userid:"1003"}, //條件{$set:{likenum:NumberInt(889)}}, //修改{multi:true} //操作參數(shù)(這里選擇修改多個(gè)符合條件的文檔) )3.4刪
刪除操作十分簡(jiǎn)單,跟上面的套路一樣,有分刪除一條和多條。
remove(),delete()One,deleteMany()方法
//綜合 db.collection.remove(<query>,{justOne: <boolean>, //刪除一條writeConcern: <document>, collation: <document>} ) //刪除一條 db.collection.deleteOne(<query>,{writeConcern: <document>,collation: <document>,hint: <document|string> // Available starting in MongoDB 4.4} ) //刪除多條 db.collection.deleteMany(<query>,{writeConcern: <document>,collation: <document>} )列子:
//刪除_id=1的 db.collection.remove({_id:"1"}) //刪除全部 db.collection.remove({}) //只刪除userid=1的信息 db.collection.deleteOne({userid:1}) //刪除多條name="zhangsan"的文檔 db.collection.deleteMany({name:"zhangsan"})4.索引操作
? 索引支持在MongoDB中高效地執(zhí)行查詢。如果沒(méi)有索引,MongoDB就必須執(zhí)行全集合掃描,以選擇與查詢語(yǔ)句匹配的文檔。MongoDB索引使用B樹(shù)數(shù)據(jù)結(jié)構(gòu)。
查看索引
db.collection.getIndexes()mongodb會(huì)給每個(gè)collection創(chuàng)建一個(gè)索引,默認(rèn)為以_id字段建立,也就是(機(jī)器碼+時(shí)間戳)的默認(rèn)字段。
創(chuàng)建索引
db.collection.createIndex({"a": 1 //索引字段},{unique: true, //條件sparse: true,expireAfterSeconds: 3600} )創(chuàng)建
//以u(píng)serid升序創(chuàng)建索引 db.collection.createIndex({userid:1}) //以u(píng)serid升序,name降序創(chuàng)建索引 db.collection.createIndex({userid:1,name:-1})刪除索引
db.collection.dropIndex(index) //例子:刪除以u(píng)serid為升序的索引 db.collection.dropIndex({userid:1})//刪除所有索引 db.collection.dropIndexes()查看執(zhí)行計(jì)劃
通常,我們想知道,建立的索引是否有效,效果如何,都需要通過(guò)執(zhí)行計(jì)劃查看。
db.collection.find(query,options).explain(options) //結(jié)果看"winningPlan"中的"stage", //如果為"COLLSCAN"就是全集合搜索,如果是 "IXSCAN"就是基于索引掃描總結(jié)
以上是生活随笔為你收集整理的mongodb shell基础命令的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 消息称谷歌地图正在开发聊天机器人,具体用
- 下一篇: 惠普新款战 99 台式机上架:i5-13