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:~#
posted @ 2022-06-30 22:11  李三幺  阅读(472)  评论(0编辑  收藏  举报