简介
由于非关系型数据库的数据没有特定的形式要求,与关系型数据库(如:MySQL、Oracle)使用SQL(Structured Query Language)语句查询数据不同,所以非关系型数据库一般都不支持SQL语句。 MongoDB作为非关系型数据库的代表,MongoDB数据库自带MongoDB shell,类似于JavaScript语法,开发人员可以通过shell与数据库实例进行交互进行数据交互
运行MongoDB shell
安装MongoDB后,在linux任意位置输入mongo 命令,即可进入,shell启动时会自动连接到本地MongoDB数据库,效果如下:
root@cbbadf81d558:~
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,效果如下:
root@cbbadf81d558:~
MongoDB shell version v5.0.7
connecting to: mongodb://127.0.0.1:27017/MongoStudy
也可以通过参数--nodb 指定不连接任何mongo数据库服务器,进入shell后手动连接,效果如下:
root@cbbadf81d558:~
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 db 或show collections 命令)不能在脚本文件中使用。不过,每个辅助函数都有对应的js等价函数可使用。常见的等价函数包括:
辅助函数 | 替代函数 |
---|
use video | db.getSisterDB(“video”) | show dbs | db.getMongo().getDBs() | show collections | db.getCollectionNames() |
可以通过mongo 脚本名称 命令在本地mongo中执行js脚本,效果如下:
root@cbbadf81d558:~
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:~
hello mongodb
root@cbbadf81d558:~
总结
本文设计到的MongoDB命令有:
mongo mongo --nodb mongo ip:port/db db mongo js文件名 --quiet mongo ip:port/db js文件名 use db show dbs show collections
|