MongoDB shell入門介紹

MongoDB 自帶 JavaScript shell,允許使用命令行與 MongoDB 實例進行交互。

運行MongoDB shell

安裝MongoDB後,在linux任意位置輸入mongo命令,即可進入,shell啓動時會自動連接到本地MongoDB數據庫,效果如下:

root@cbbadf81d558:~# mongo
MongoDB shell version v5.0.7
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("beb55bf8-8533-40f0-a957-e27b28e744eb") }
MongoDB server version: 5.0.7
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
        2022-06-27T05:24:33.178+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2022-06-27T05:24:33.310+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-06-27T05:24:33.310+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

也可以通過mongo ip:port[/collectionName]命令指定連接到其他機器上的MongoDB,效果如下:

# 其中[/collectionName]爲可選項,可以不寫,默認進入test集合
root@cbbadf81d558:~# mongo 127.0.0.1:27017/MongoStudy
MongoDB shell version v5.0.7
connecting to: mongodb://127.0.0.1:27017/MongoStudy

也可以通過參數--nodb指定不連接任何mongo數據庫服務器,進入shell後手動連接,效果如下:

root@cbbadf81d558:~# mongo --nodb
MongoDB shell version v5.0.7
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
>

在shell中鏈接mongo數據庫服務的方式爲:

> let conn=new Mongo("127.0.0.1:27017")
> db=conn.getDB("MongoStudy")
MongoStudy
> db
MongoStudy
>

使用MongoShell執行JavaScript腳本

可以在shell中傳入js腳本,以執行提前定義好的邏輯。注意,在腳本中可以訪問mongo提供的全局變量,但是輔助函數(如use dbshow collections命令)不能在腳本文件中使用。不過,每個輔助函數都有對應的js等價函數可使用。常見的等價函數包括:

輔助函數 替代函數
use video db.getSisterDB("video")
show dbs db.getMongo().getDBs()
show collections db.getCollectionNames()

可以通過mongo 腳本名稱命令在本地mongo中執行js腳本,效果如下:

root@cbbadf81d558:~# mongo ./script.js
MongoDB shell version v5.0.7
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("972ea736-5895-448f-9042-4c8f7b800214") }
MongoDB server version: 5.0.7
hello mongodb
root@cbbadf81d558:~#

或通過mongo 127.0.0.1:27017/MongoStudy ./script.js命令,指定其他服務器上的mongo執行js腳本。也可以使用參數--quiet關閉系統打印日誌,只顯示js腳本的輸出內容,效果如下:

root@cbbadf81d558:~# mongo ./script.js --quiet
hello mongodb
root@cbbadf81d558:~#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章