亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mongodb命令行連接及基礎(chǔ)命令總結(jié)大全

 更新時間:2024年04月17日 09:52:18   作者:最后一支迷迭香  
大家可能平時在開發(fā)過程中都使用客戶端工具來連接和查詢mongodb,但是一般生產(chǎn)當(dāng)中的數(shù)據(jù)庫是不允許本地客戶端連接的,下面這篇文章主要給大家介紹了關(guān)于mongodb命令行連接及基礎(chǔ)命令總結(jié)的相關(guān)資料,需要的朋友可以參考下

一、命令行連接到mongodb

我們先來簡單的看一下mongosh命令:

root@bddff4197a79:/# mongosh --help

  $ mongosh [options] [db address] [file names (ending in .js or .mongodb)]

  Options:

    -h, --help                                 Show this usage information
    -f, --file [arg]                           Load the specified mongosh script
        --host [arg]                           Server to connect to
        --port [arg]                           Port to connect to
        --version                              Show version information
        --verbose                              Increase the verbosity of the output of the shell
        --quiet                                Silence output from the shell during the connection process
        --shell                                Run the shell after executing files
        --nodb                                 Don't connect to mongod on startup - no 'db address' [arg] expected
        --norc                                 Will not run the '.mongoshrc.js' file on start up
        --eval [arg]                           Evaluate javascript
        --retryWrites                          Automatically retry write operations upon transient network errors

  Authentication Options:

    -u, --username [arg]                       Username for authentication
    -p, --password [arg]                       Password for authentication
        --authenticationDatabase [arg]         User source (defaults to dbname)
        --authenticationMechanism [arg]        Authentication mechanism
        --awsIamSessionToken [arg]             AWS IAM Temporary Session Token ID
        --gssapiServiceName [arg]              Service name to use when authenticating using GSSAPI/Kerberos
        --sspiHostnameCanonicalization [arg]   Specify the SSPI hostname canonicalization (none or forward, available on Windows)
        --sspiRealmOverride [arg]              Specify the SSPI server realm (available on Windows)

  TLS Options:

        --tls                                  Use TLS for all connections
        --tlsCertificateKeyFile [arg]          PEM certificate/key file for TLS
        --tlsCertificateKeyFilePassword [arg]  Password for key in PEM file for TLS
        --tlsCAFile [arg]                      Certificate Authority file for TLS
        --tlsAllowInvalidHostnames             Allow connections to servers with non-matching hostnames
        --tlsAllowInvalidCertificates          Allow connections to servers with invalid certificates
        --tlsCertificateSelector [arg]         TLS Certificate in system store (Windows and macOS only)
        --tlsCRLFile [arg]                     Specifies the .pem file that contains the Certificate Revocation List
        --tlsDisabledProtocols [arg]           Comma separated list of TLS protocols to disable [TLS1_0,TLS1_1,TLS1_2]

  API version options:

        --apiVersion [arg]                     Specifies the API version to connect with
        --apiStrict                            Use strict API version mode
        --apiDeprecationErrors                 Fail deprecated commands for the specified API version

  FLE Options:

        --awsAccessKeyId [arg]                 AWS Access Key for FLE Amazon KMS
        --awsSecretAccessKey [arg]             AWS Secret Key for FLE Amazon KMS
        --awsSessionToken [arg]                Optional AWS Session Token ID
        --keyVaultNamespace [arg]              database.collection to store encrypted FLE parameters
        --kmsURL [arg]                         Test parameter to override the URL of the KMS endpoint

  DB Address Examples:

        foo                                    Foo database on local machine
        192.168.0.5/foo                        Foo database on 192.168.0.5 machine
        192.168.0.5:9999/foo                   Foo database on 192.168.0.5 machine on port 9999
        mongodb://192.168.0.5:9999/foo         Connection string URI can also be used

  File Names:

        A list of files to run. Files must end in .js and will exit after unless --shell is specified.

  Examples:

        Start mongosh using 'ships' database on specified connection string:
        $ mongosh mongodb://192.168.0.5:9999/ships

  For more information on usage: https://docs.mongodb.com/mongodb-shell.

可以看到mongosh有非常多的參數(shù),下面我們演示幾個比較常用的參數(shù)來測試連接mongo連接到mongodb命令

  • –host為遠程服務(wù)器地址及端口
  • -u為用戶名
  • -p為密碼
mongosh --host localhost:27017 -u root -p 'yourpassword'

如果沒有密碼可直接使用

mongosh --host localhost:27017 

demo:

root@bddff4197a79:/# mongosh --host localhost:27017
Current Mongosh Log ID:	654b58d5a9821e4d7bbbf493
Connecting to:		mongodb://localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:		5.0.5
Using Mongosh:		1.1.6

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting:
   2023-11-08T01:13:10.562+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2023-11-08T01:13:11.610+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

Warning: Found ~/.mongorc.js, but not ~/.mongoshrc.js. ~/.mongorc.js will not be loaded.
  You may want to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.

二、基礎(chǔ)命令

2.1 數(shù)據(jù)庫操作

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

show dbs

查看當(dāng)前所屬數(shù)據(jù)庫

db

切換數(shù)據(jù)庫或創(chuàng)建數(shù)據(jù)庫(存在則切換,不存在則創(chuàng)建)

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

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

db.dropDatabase()

2.2 集合的基礎(chǔ)命令

不手動創(chuàng)建集合:

  • 向不存在的集合中第一次加入數(shù)據(jù)時,集合會被創(chuàng)建出來

手動創(chuàng)建集合:

db.createCollection(name, options)
db.createCollection(“stu”)
db.createCollection(“stb”, {capped:true, size:10})

  • 參數(shù)crapped:默認(rèn)值為false表示不設(shè)置上限,值為true表示設(shè)置上限
  • 參數(shù)size:當(dāng)capped值為true時,需要指定此參數(shù),表示上限大小,當(dāng)文檔達到上限時,會將之前的數(shù)據(jù)覆蓋,單位為字節(jié)

查看集合:

show collections

刪除結(jié)合:

db.集合名稱.drop()

2.3 插入

db.集合名稱.insert(document)

db.stu.insert({name:'zhangsan', gender:1})
db.stu.insert({_id:"20170101", name:'zhangsan', gender:1})

插入文檔時,如果不指定_id參數(shù),MongoDB會為文檔分配一個唯一的Objectid

2.4 保存

db.集合名稱.save(document)

如果文檔的_id已經(jīng)存在則修改,如果文檔的_id不存在則添加

2.5 查詢

方法 find() 查詢?nèi)?/p>

	db.集合名稱.find({條件文檔})
	db.stu.find({name:'zhangsan'})

方法 findOne() 查詢只返回一個

    db.集合名稱.findOne({條件文檔})
    db.stu.findOne({age:20})

方法 pretty() 將結(jié)果格式化

    db.集合名稱.find({條件文檔}).pretty()
    db.stu.find({name:'zhangsan'}).pretty()

2.6 更新

db.集合名稱.update(,,{multi:})

1. 參數(shù)query:查詢條件

2. 參數(shù)update:更新操作符

3. 參數(shù)multi:可選,默認(rèn)是false,表示只更新找到的第一條記錄,值為true表示把滿足條件的文檔全部更新

	db.stu.update({name:'zhangsan', {name:'wangwu'}}) 更新匹配的第一條,其他值會被刪除
    db.stu.update({name:'zhangsan', {$set:{name:'hys'}}}) 更新匹配的第一條,其他值不會被刪除
    db.stu.update{{}, {$set:{gender:0}}, {multi:true}} 跟新全部,其他值不會被刪除

2.7 刪除

db.集合名稱.remove(, {justOne:})

1. 參數(shù)query:可選,刪除文檔的條件

2. 參數(shù)justOne:可選,如果設(shè)為true或1,則只刪除一條,默認(rèn)為false,表示刪除多條

	db.stu.remove({name:'wangwu'}, {justOne:true}) 刪除一條
	db.stu.remove({gender:2}) 刪除全部

2.8 比較運算符

等于:默認(rèn)是等于判斷,沒有運算符

小于: l t ( l e s s t h a n ) 小于等于: lt (less than) 小于等于: lt(lessthan)小于等于:lte (less than equal)

大于: g t ( g r e a t e r t h a n ) 大于等于: gt (greater than) 大于等于: gt(greaterthan)大于等于:gte (greater than equal)

不等于:$ne (not equal)

db.stu.find({age: {$gte:18}})

2.9 范圍運算符

使用 “ i n " , " in", " in","nin” 判斷是否在某個范圍內(nèi)

查詢年齡為18、28的學(xué)生

db.stu.find({age:{$in:[18,28]}})

2.10 邏輯運算符

and:在json中寫多個條件即可

查詢年齡大于或等于18,并且性別為1的學(xué)生

db.stu.find({age:{$gte:18}, sex:1})

or:使用 “$or” ,值為數(shù)組,數(shù)組中每個元素為json

查詢年齡大于18,或性別為1的學(xué)生

db.stu.find({$or:[{age:{$gt:18}}, {sex:1}]})

查詢年齡大于18,或性別為1的學(xué)生,并且姓名是xiaoming

db.stu.find({$or:[{age:{$gt:18}}, {set:1}],name:'xiaoming'})

2.11 正則表達式

使用 // 或 $regex 編寫正則表達式

查詢姓小的學(xué)生

db.stu.find({name:/^xiao/})
db.stu.find({name:{$regex:'^xiao'}})

2.12 limit和skip()

方法limit():用于讀取指定數(shù)量的文檔

db.集合名稱.find().limit(number)

查詢2條學(xué)生信息

db.stu.find().limit(2)

方法skip():用于跳過指定數(shù)量的文檔

db.集合名稱.find().skip(number)

db.stu.find().skip(2)

同時使用

db.stu.find().limit(1).skip(2)
db.stu.find().limit(2).skip(1)

2.13 排序

方法 sort() 用于對集合進行排列

db.集合名稱.find().sort({字段:1,…})

  • 參數(shù) 1 為升序排列
  • 參數(shù) -1 位降序排列

根據(jù)性別降序,再根據(jù)年齡升序

db.stu.find().sort({sex:-1,age:1})

2.14 統(tǒng)計個數(shù)

方法 count() 用于統(tǒng)計結(jié)果集中文檔條數(shù)

db.集合名稱.find({條件}).count()

db.集合名稱.count({條件})

db.stu.find({sex:1}).count()
db.stu.count({age:{$gt:20},sex:1})

2.15 消除重復(fù)

方法 distinct() 對數(shù)據(jù)進行去重

db.集合名稱.distinct(‘去重字段’,{條件})

db.stu.distinct('sex', {age: {$gt:18}})

三、聚合函數(shù)操作

3.1常用管道

在mongodb中,文檔處理完畢后,通過管道進行下一次處理

常用的管道如下:

  • $group:將集合中的文檔分組,可用于統(tǒng)計結(jié)果
  • $match:過濾數(shù)據(jù),只輸出符合條件的文檔
  • $project:修改輸入文檔的結(jié)構(gòu),如重命名、增加、刪除字段、創(chuàng)建計算結(jié)果
  • $sort:講輸入文檔排序后輸出
  • $limit:限制聚合管道返回的文檔數(shù)
  • $skip:跳過指定數(shù)量的文檔。并返回余下的文檔
  • $unwind:將數(shù)組類型的字段進行拆分

3.2 表達式

常用表達式:

  • $sum:計算總和, $sum:1 表示以一倍計算
  • $avg:計算平均值
  • $min:獲取最小值
  • $max:獲取最大值
  • $push:在結(jié)果文檔中插入值到一個數(shù)組中
  • $first:根據(jù)資源文檔的排序獲取第一個文檔數(shù)據(jù)
  • $last:根據(jù)資源文檔的排序獲取最后一個文檔數(shù)據(jù)

3.3 $group

將集合中的文檔分組,可用于統(tǒng)計結(jié)果

_id表示分組的依據(jù),使用某個字段的格式為’$字段’

統(tǒng)計地區(qū)總數(shù)

db.map.aggregate(
    {$group:{_id: '$country',counter: {$sum:1}}}
)

將集合中所有文檔分為一組:求學(xué)生的總?cè)藬?shù)和平均年齡

db.stu.aggregate(
    {$group:{_id: null , counter: {$sum:1},age_avg: {$avg: '$age'}}}
)

3.4 $project

查詢學(xué)生的姓名、年齡

db.stu.aggregate(
   {$project:{_id:0,name:1,age:1}}
)

查詢男生、女生人數(shù),輸出人數(shù)

db.stu.aggregate(
   {$group:{_id:'$sex',counter:{$sum:1}}},
   {$project:{_id:0, sex:'$_id',counter:1}}
)

3.5 $match

用于過去數(shù)據(jù),只輸出符合條件的文檔

查詢年齡大于20的學(xué)生

db.stu.aggregate(
   {$match:{age:{$gt:20}}}
)

查詢你哪里大于20的男生、女生總數(shù)

db.stu.aggregate(
   {$match:{age:{$gt:20}}},
   {$group:{_id:'$sex', counter:{$sum:1}}}
)

3.6 $sort

將輸入文檔排序后輸出

查詢學(xué)生信息,并按年齡升序

db.stu.aggregate(
    {$sort:{age:1}}
)

按照性別分類并按人數(shù)降序

db.stu.aggregate(
    {$group:{_id:'$sex',count:{$sum:1}}},
    {$sort:{count:-1}},
    {$project:{count:1,_id:0}})

3.7 $limit

限制聚合管道返回的文檔數(shù)

查詢2條學(xué)生信息

db.stu.aggregate(
   {$limit:2}
)

3.8 $skip

跳過指定數(shù)量的文檔,并返回余下的文檔

查詢從第2條開始的學(xué)生信息

db.stu.aggregate(
   {$skip:2}
)

3.9 $unwind

將文檔中的某一個數(shù)組類型字段拆分成多條,每條包含數(shù)組中的一個值

語法:db.集合名稱.aggregate({ u n w i n d : ′ unwind:' unwind:′字段名稱’})

db.t2.insert(
   {_id:1, item:'t-shirt', size:['S', 'M', 'L']}
)
db.t2.aggregate(
   {$unwind:'$size'}
)
注意:如果每條文檔中含有該數(shù)組的字段值為空的時候,想保留字段內(nèi)容,可以使用:
db.t2.aggregate(
   {$unwind:{
       path: '$字段名稱',
       preserveNullAndEmptyArrays:<boolean>  # 防止數(shù)據(jù)丟失
   }}
)

總結(jié) 

到此這篇關(guān)于mongodb命令行連接及基礎(chǔ)命令總結(jié)的文章就介紹到這了,更多相關(guān)mongodb命令行連接基礎(chǔ)命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MongoDB安裝圖文教程

    MongoDB安裝圖文教程

    這篇文章主要為大家詳細(xì)介紹了MongoDB安裝圖文教程,分為兩大部分為大家介紹下載MongoDB和安裝MongoDB的方法,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Mongodb解決不能連接到服務(wù)器的錯誤問題

    Mongodb解決不能連接到服務(wù)器的錯誤問題

    這篇文章主要介紹了Mongodb解決不能連接到服務(wù)器的錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 關(guān)于NoSQL之MongoDB的一些總結(jié)

    關(guān)于NoSQL之MongoDB的一些總結(jié)

    這篇文章主要介紹了關(guān)于NoSQL之MongoDB的一些總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • mongodb增量/全量備份腳本的實現(xiàn)詳解

    mongodb增量/全量備份腳本的實現(xiàn)詳解

    這篇文章主要給大家介紹了關(guān)于mongodb增量/全量備份腳本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • MongoDB整庫備份與還原以及單個collection備份、恢復(fù)方法

    MongoDB整庫備份與還原以及單個collection備份、恢復(fù)方法

    mongodb數(shù)據(jù)庫維護離不開必要的備份、恢復(fù)操作,而且一般不會出錯,所以我們在使用的時候大部分時候使用備份和恢復(fù)操作就可以了
    2013-08-08
  • 詳解linux 使用docker安裝mongodb方法

    詳解linux 使用docker安裝mongodb方法

    這篇文章主要介紹了linux 使用docker安裝mongodb方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • MongoDB 副本集的搭建過程

    MongoDB 副本集的搭建過程

    這篇文章主要介紹了MongoDB 副本集的搭建過程,幫助大家更好的理解和學(xué)習(xí)使用MongoDB數(shù)據(jù)庫,感興趣的朋友可以了解下
    2021-03-03
  • MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法

    MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法

    MongoDB?是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。接下來通過本文給大家介紹MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法,感興趣的朋友一起看看吧
    2021-11-11
  • MongoDB對數(shù)組進行增刪改查操作

    MongoDB對數(shù)組進行增刪改查操作

    與關(guān)系型數(shù)據(jù)庫相比,MongoDB支持?jǐn)?shù)組,將數(shù)組存儲到文檔之中,下面這篇文章主要給大家介紹了關(guān)于MongoDB對數(shù)組進行增刪改查操作的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • MongoDB數(shù)據(jù)庫性能監(jiān)控詳解

    MongoDB數(shù)據(jù)庫性能監(jiān)控詳解

    MongoDB作為圖片和文檔的存儲數(shù)據(jù)庫,為啥不直接存MySQL里,還要搭個MongoDB集群,麻不麻煩?這篇文章就帶你介紹MongoDB數(shù)據(jù)庫性能監(jiān)控,感興趣的同學(xué)可以參考閱讀
    2023-03-03

最新評論