日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mongodb shell基础命令

發(fā)布時間:2024/9/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongodb shell基础命令 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

mongodb shell命令

1.數(shù)據(jù)庫基本操作

在mongodb中,使用use來創(chuàng)建和選擇數(shù)據(jù)庫,當數(shù)據(jù)庫不存在時,use會創(chuàng)建一個新的數(shù)據(jù)庫,但是這數(shù)據(jù)庫并沒有持久化到硬盤里面,而存在內存中,只有當用戶往這個新創(chuàng)建的空數(shù)據(jù)庫建collection才會做持久化操作。

創(chuàng)建和切換數(shù)據(jù)庫

use 數(shù)據(jù)庫名稱

查看有權限查看的所有的數(shù)據(jù)庫

mongodb是不對空數(shù)據(jù)庫做持久化的,所以新創(chuàng)建的空數(shù)據(jù)庫,用show指令是看不見的,必須建collection后才能看見。

show dbs show databases

查看當前正在使用的數(shù)據(jù)庫

在mongo中db就代表了你當前正在使用的數(shù)據(jù)庫對象。

db

刪除數(shù)據(jù)庫

db代表當前正在使用的數(shù)據(jù)庫對象,刪除數(shù)據(jù)庫前,先要切換到要刪除的數(shù)據(jù)庫。主要用來刪除已經(jīng)持久化的數(shù)據(jù)庫。

db.dropDatabase()

查看數(shù)據(jù)庫用戶

show users

2.集合的基本操作

在mongodb中集合(collection)對應mysql中的表(table)

顯式創(chuàng)建collection

db.createCollection("CollectionName")

查看collection

可以看到mongodb一些操作命令是跟mysql兼容的。

show collections show tables

隱式創(chuàng)建collection

當我們向一個不存在的collection插入一個文檔(document)時,會先隱式創(chuàng)建一個collection,再進行數(shù)據(jù)的插入。

db.myCollection.insertOne( { name: "xiaoming" } );

3.增刪改查

在mongodb中每一行數(shù)據(jù)對應一個文檔(document),每個文檔是一種類似于 JSON 的 格式叫BSON,所以它既可以存儲比較復雜的數(shù)據(jù)類型,又相當?shù)撵`活。BSON可以是二進制形式的JSON。MongoDB中的記錄是一個文檔,它是一個由字段和值對(fifield:value)組成的數(shù)據(jù)結構。MongoDB文檔類似于JSON對象,即一個文檔認為就是一個對象。字段的數(shù)據(jù)類型是字符型,它的值除了使用基本的一些類型外,還可以包括其他文檔、普通數(shù)組和文檔數(shù)組。

3.1 增

插入一個文檔

insertOne是用來插入一個文檔的,當傳入多個文檔文檔對象時,就會報錯。

用法:

db.collectionName.insertOne(<document>, { writeConcern:<document>} )
  • writeConcern:寫關注,可選。
db.inventory.insertOne({ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } } )

插入多個文檔

insertMany用來插入多個文檔(document),傳入的是一個數(shù)組對象,里面包含了多個插入對象。

用法:

db.collectionName.insertOne( [<document1>,<document2>], { writeConcern:<document>,ordered:boolean} )
  • writeConcern:寫關注,可選。
  • ordered:執(zhí)行有序插入還是無序插入。默認為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" } } ])

插入一個或多個

insert方法可以插入一個document也可以插入多個document,這里就不多說了,當成insertMany+insertOne綜合來用就可以了。

用法:

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" } } ) #傳入一個{name:1}來表示指定顯示name字段,其他字段不顯示 db.collection.find( { size: { h: 14, w: 21, uom: "cm" } },{name:1} )

查詢一條信息

查詢一條信息用findOne()方法,調用此方法,返回的是查詢到的第一條信息,當找到第一條信息時就不會繼續(xù)往下找了。

用法:

db.collectionName.findOne(query,projection)

例子:

#查詢size: { h: 14, w: 21, uom: "cm" }的文檔,找到立即返回 db.collection.find( { size: { h: 14, w: 21, uom: "cm" } } )

分頁查詢

limit(num),指定要查詢的條數(shù)。

skip(num),指定跳過的條數(shù)

//查詢前面前前三條數(shù)據(jù) db.collection.find().limit(3) //從第2條開始查詢,查到第5條 db.collection.find().skip(2).limit(3)

排序查詢

sort() 方法對數(shù)據(jù)進行排序,sort() 方法可以通過參數(shù)指定排序的字段,并使用 1 和 -1 來指定排序的方式,其中 1 為升序排列,而 -1 是用于降序排列。

//按userid的降序 db.collection.find().sort({userid:-1}) //按userid的升序 db.collection.find().sort({userid:1})

正則查詢

mongodb支持正則表達式,放在 / 之間

db.collect.find({userid:/正則表達式子/})

注意:skip(), limilt(), sort()三個放在一起執(zhí)行的時候,執(zhí)行的順序是先 sort(), 然后是 skip(),最后是顯示的 limit()。

條件查詢

查詢時我們往往需要一些比較條件,>,<,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 不等于
db.集合名稱.find({userid:{$in:["1003","1004"]}}) db.集合名稱.find({userid:{$nin:["1003","1004"]}})
  • $in 包含
  • $nin 不包含
$and:[ { },{ },{ } ] //例子: db.集合名稱.find({$and[{userid:{$gt:NumberInt(700)}},{name:"zhangsan"}] }) $or:[ { },{ },{ } ] //例子: db.集合名稱.find({$or[{userid:{$lt:100}},{name:"zhangsan"}] })

3.3改

使用update()方法可以進行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:更改的信息(默認會直接覆蓋整個文檔)
  • upsert:可選。如果設置為true,則在沒有與查詢條件匹配的文檔時創(chuàng)建新文檔。默認值為false,如果找不到匹配項,則不會插入新文檔。
  • multi :可選。如果設置為true,則更新符合查詢條件的多個文檔。如果設置為false,則更新一個文檔。默認值為false。

例子:

//使用$set符可以只修改文檔中的likenum字段,而非直接將整個文檔覆蓋 db.collection.update({_id:"2"},{$set:{likenum:NumberInt(889)}}) //查詢所有包含userid=1003的信息,并修改其中的likenum字段,multi為更新符合查詢條件的多個文檔 db.collection.update({userid:"1003"}, //條件{$set:{likenum:NumberInt(889)}}, //修改{multi:true} //操作參數(shù)(這里選擇修改多個符合條件的文檔) )

3.4刪

刪除操作十分簡單,跟上面的套路一樣,有分刪除一條和多條。

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í)行查詢。如果沒有索引,MongoDB就必須執(zhí)行全集合掃描,以選擇與查詢語句匹配的文檔。MongoDB索引使用B樹數(shù)據(jù)結構。

查看索引

db.collection.getIndexes()

mongodb會給每個collection創(chuàng)建一個索引,默認為以_id字段建立,也就是(機器碼+時間戳)的默認字段。

創(chuàng)建索引

db.collection.createIndex({"a": 1 //索引字段},{unique: true, //條件sparse: true,expireAfterSeconds: 3600} )

創(chuàng)建

//以userid升序創(chuàng)建索引 db.collection.createIndex({userid:1}) //以userid升序,name降序創(chuàng)建索引 db.collection.createIndex({userid:1,name:-1})

刪除索引

db.collection.dropIndex(index) //例子:刪除以userid為升序的索引 db.collection.dropIndex({userid:1})//刪除所有索引 db.collection.dropIndexes()

查看執(zhí)行計劃

通常,我們想知道,建立的索引是否有效,效果如何,都需要通過執(zhí)行計劃查看。

db.collection.find(query,options).explain(options) //結果看"winningPlan"中的"stage", //如果為"COLLSCAN"就是全集合搜索,如果是 "IXSCAN"就是基于索引掃描

總結

以上是生活随笔為你收集整理的mongodb shell基础命令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。