【Mongo】初步认识MongoDB shell


系列文章索引:
【Mongo】MongoDB文档的增删改操作
【Mongo】MongoDB文档查询

简介

由于非关系型数据库的数据没有特定的形式要求,与关系型数据库(如:MySQL、Oracle)使用SQL(Structured Query Language)语句查询数据不同,所以非关系型数据库一般都不支持SQL语句。
MongoDB作为非关系型数据库的代表,MongoDB数据库自带MongoDB shell,类似于JavaScript语法,开发人员可以通过shell与数据库实例进行交互进行数据交互

运行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 videodb.getSisterDB(“video”)
show dbsdb.getMongo().getDBs()
show collectionsdb.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:~#

总结

本文设计到的MongoDB命令有:

  1. mongo
  2. mongo --nodb
  3. mongo ip:port/db
  4. db
  5. mongo js文件名 --quiet
  6. mongo ip:port/db js文件名
  7. use db
  8. show dbs
  9. show collections
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李奇技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值