最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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:默認值為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:可選,默認是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,則只刪除一條,默認為false,表示刪除多條

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

2.8 比較運算符

等于:默認是等于判斷,沒有運算符

小于: 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中實現(xiàn)多表聯(lián)查的實例教程

    MongoDB中實現(xiàn)多表聯(lián)查的實例教程

    數(shù)據(jù)庫應(yīng)用在我們的生活中是很常見的,在編輯一些應(yīng)用以及軟件的時候都需要用到數(shù)據(jù)庫來存儲數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MongoDB中實現(xiàn)多表聯(lián)查的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • 在MongoDB中實現(xiàn)大于小于查詢功能

    在MongoDB中實現(xiàn)大于小于查詢功能

    MongoDB是一個高性能、開源、無模式的文檔型數(shù)據(jù)庫,它使用BSON(Binary JSON)作為存儲格式,支持豐富的查詢語法,本文將詳細介紹如何在MongoDB中實現(xiàn)大于小于查詢,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下
    2024-08-08
  • mongodb3.4集群搭建實戰(zhàn)之高可用的分片+副本集

    mongodb3.4集群搭建實戰(zhàn)之高可用的分片+副本集

    這篇文章主要給大家介紹了關(guān)于mongodb3.4集群搭建實戰(zhàn)之高可用的分片+副本集的相關(guān)資料,文中通過示例代碼將實現(xiàn)的步驟一步步的介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • SpringBoot?整合mongoDB并自定義連接池的示例代碼

    SpringBoot?整合mongoDB并自定義連接池的示例代碼

    這篇文章主要介紹了SpringBoot?整合mongoDB并自定義連接池?,整合mongoDB的目的就是想用它給我們提供的mongoTemplate,它可以很容易的操作mongoDB數(shù)據(jù)庫,對整合過程及實例代碼感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • MongoDB TTL索引的實例詳解

    MongoDB TTL索引的實例詳解

    這篇文章主要介紹了 MongoDB TTL索引的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • PHP  MongoDB GridFS 存儲文件的方法詳解

    PHP MongoDB GridFS 存儲文件的方法詳解

    這篇文章主要介紹了PHP MongoDB GridFS 存儲文件的方法詳解的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 詳解MongoDB中的日志模塊

    詳解MongoDB中的日志模塊

    這篇文章主要介紹了MongoDB中的日志模塊的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用MongoDB數(shù)據(jù)庫,感興趣的朋友可以了解下
    2021-04-04
  • Windows下MongoDB的下載安裝、環(huán)境配置教程圖解

    Windows下MongoDB的下載安裝、環(huán)境配置教程圖解

    這篇文章主要介紹了Windows下MongoDB的下載安裝、環(huán)境配置教程詳解,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-06-06
  • MongoDB orm框架的注意事項及簡單使用

    MongoDB orm框架的注意事項及簡單使用

    MongoDB官方提供的orm的輕量級封裝的 zfoo orm 框架,只對官方提供的進行了簡單的封裝,還做了一些官方不支持的語法校驗。本文介紹了該框架的注意事項及簡單使用
    2021-06-06
  • MongoDB中javascript腳本編程簡介和入門實例

    MongoDB中javascript腳本編程簡介和入門實例

    作為一個數(shù)據(jù)庫,MongoDB有一個很大的優(yōu)勢——它使用js管理數(shù)據(jù)庫,所以也能夠使用js腳本進行復(fù)雜的管理——這種方法非常靈活
    2014-04-04

最新評論

遂溪县| 双流县| 灵丘县| 青岛市| 永安市| 阳山县| 上林县| 肇州县| 祁门县| 昭苏县| 宕昌县| 会理县| 罗田县| 龙南县| 新干县| 莫力| 乳山市| 木兰县| 铜梁县| 自治县| 昭觉县| 获嘉县| 娱乐| 徐闻县| 昌黎县| 红河县| 浦江县| 平阳县| 沾益县| 临城县| 惠水县| 北京市| 新巴尔虎右旗| 遂宁市| 香格里拉县| 凌云县| 通海县| 景洪市| 怀化市| 广东省| 佛学|