- Published on
The new MongoDB shell, mongosh
- Authors
- Name
- interglobalmedia
- @letsbsocial1
With the (relatively) new MongoDB community version 6.0+
(6.0.1
) installed with Homebrew
on macOS
(I run on Ventura
), the old (legacy) mongo shell
has been deprecated and replaced with the new mongosh shell
. It is important to use this new shell with versions 6.0+
, because otherwise we get the following error/warning:
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/db?compressors=disabled&gssapiServiceName=mongodb
2022-11-27T08:40:29.390-0500 I CONTROL [js] machdep.cpu.extfeatures unavailable
Implicit session: session { "id" : UUID("96aa8ae3-59f9-4b64-a72c-2582b799f251") }
MongoDB server version: 6.0.1
WARNING: shell and server versions do not match
But if I run mongosh instead, I get the following:
mongosh db
Current Mongosh Log ID: 6383690f53b99d4afd657571
Connecting to: mongodb://127.0.0.1:27017/db?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB: 6.0.1
Using Mongosh: 1.6.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting
2022-11-27T08:26:32.193-05:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
------
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()
------
db>
Currently I am using MongoDB
with Next.js
, and there are some things that we have to do differently when using Mongoose
with Next.js
. I will talk about that in a later post. But the point is that I installed the latest version of the MongoDB community version
as I previously did with Homebrew
, but updated to this latest version now. And then I had to find out what was going on with there being a shell/server
mismatch. And that is when I found out from the MongoDB docs
that the legacy (mongo) shell has been deprecated, and that there now is a new one called mongosh
.
For example, to start mongosh
, which was installed with Homebrew
on macOS
, I run the following command:
mongosh
This starts the mongosh shell
, and initially returns the following in the Terminal console
:
mongosh
Current Mongosh Log ID: 63836d06a08906465f1a64e6
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB: 6.0.1
Using Mongosh: 1.6.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting
2022-11-27T08:58:28.615-05:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
------
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()
------
test>
So by default, it takes me into a (local
) db
called “test”
. I can check this by running the following command:
db
and “test”
is returned. But let’s say I wanted to switch to another database``, but I am ***not sure*** which
databases` I have at this time. First I run:
show dbs
And it returns:
admin 40.00 KiB
bookit 72.00 KiB
config 72.00 KiB
local 72.00 KiB
So those are my current local dbs
on my computer.
Then let’s say that I want to switch into the bookit db
. I run the following command:
use bookit
And the following is returned in the Terminal console
:
switched to db bookit
bookit>
And if I want to get out of mongosh``, I ***use*** the ***keyboard shortcut***
control key + D key, which ***takes*** me ***out of*** the
mongosh shell`. And then I run
brew services stop mongodb-community
to stop the mongoldb-community service
altogether.
Happy mongoshing!
Related Resources
- The MongoDB Shell versus the Legacy mongo Shell: MongoDB documentation
Run (mongosh) Commands: MongoDB documentation