1+ :template: product-landing
2+ :hidefeedback: header
3+ :noprevnext:
4+
15.. program:: mongosh
26
37.. binary:: mongosh
48
59.. _mdb-shell-overview:
610
7- ===========================
8- MongoDB Shell (``mongosh``)
9- ===========================
11+ ======================================
12+ Welcome to MongoDB Shell (``mongosh``)
13+ ======================================
14+
15+ .. introduction::
16+
17+ The |mdb-shell|, ``mongosh``, is a JavaScript and Node.js |repl|
18+ environment for interacting with MongoDB deployments in
19+ `Atlas <https://www.mongodb.com/docs/atlas/>`__, locally,
20+ or on another remote host. Use the |mdb-shell| to test queries and
21+ interact with the data in your MongoDB database.
22+
23+ .. button:: Download mongosh
24+ :uri: https://www.mongodb.com/try/download/shell?jmp=docs
25+
26+ .. image:: /images/mongosh-plp/Technical_SOFTWARE_Terminal(3)_Spot.png
27+ :alt: Abstract illustration showing a terminal interacting with different systems
28+
29+ .. kicker:: What You Can Do
30+
31+ Access MongoDB From Your Shell
32+ ------------------------------
33+
34+ .. tabs::
35+
36+ .. tab:: Connect to MongoDB
37+ :tabid: connect
38+
39+ .. procedure::
40+
41+ .. step:: Find Your Connection String
42+
43+ Find your connection string. The connection string varies
44+ depending on the type of deployment you're connecting to.
45+
46+ Learn how to find your connection string for
47+ `Atlas <https://www.mongodb.com/docs/manual/reference/connection-string/#find-your-connection-string>`__.
48+
49+ Or connect to an
50+ `self-hosted <https://www.mongodb.com/docs/manual/reference/connection-string/#find-your-self-hosted-deployment-s-connection-string>`__
51+ deployment.
52+
53+ .. step:: Connect to MongoDB
54+
55+ Connect to a MongoDB deployment using the
56+ connection string.
57+
58+ The following connection string connects to
59+ an Atlas deployment:
60+
61+ .. code-block:: sh
62+
63+ mongosh "mongodb+srv://mycluster.abcd1.mongodb.net/myFirstDatabase" --apiVersion 1 --username <username>
64+
65+ .. step:: Interact with Your Data
66+
67+ Use your chosen connection type to view your data, import
68+ documents, and run queries.
69+
70+ For more information, refer to :ref:`mdb-shell-crud`.
71+
72+ .. image:: /images/mongosh-plp/atlas_plp.gif
73+ :alt: MongoDB Atlas Connect to your Database image
74+
75+ .. tab:: Perform CRUD and Aggregation
76+ :tabid: crud
77+
78+ .. procedure::
79+
80+ .. step:: Insert Documents
81+
82+ :binary:`~bin.mongosh` supports common insert opererations,
83+ including:
84+
85+ - :method:`db.collection.insertOne()`
86+ - :method:`db.collection.insertMany()`
87+
88+ For more information and examples, refer to :ref:`mongosh-insert`.
89+
90+ .. step:: Read Documents
91+
92+ Use the :method:`db.collection.find()` method to query documents
93+ in a collection. For more information and examples, refer to
94+ :ref:`mongosh-read`.
95+
96+ .. step:: Update Documents
97+
98+ :binary:`~bin.mongosh` supports common update operations,
99+ including:
100+
101+ - :method:`db.collection.updateOne()`
102+ - :method:`db.collection.updateMany()`
103+ - :method:`db.collection.replaceOne()`
104+
105+ For more information and examples, refer to :ref:`mongosh-update`.
106+
107+ .. step:: Delete Documents
108+
109+ :binary:`~bin.mongosh` supports common delete operations,
110+ including:
111+
112+ - :method:`db.collection.deleteMany()`
113+ - :method:`db.collection.deleteOne()`
114+
115+ For more information and examples, refer to :ref:`mongosh-delete`.
116+
117+ .. step:: Run Aggregation Pipelines
118+
119+ You can run aggregation pipelines in :binary:`~bin.mongosh`
120+ using the :method:`db.collection.aggregate()` method.
121+ Aggregation pipelines transform your documents into
122+ aggregated results based on the stages you specify. For more
123+ information and examples, refer to :ref:`mdb-shell-aggregation`.
124+
125+ .. image:: /images/mongosh-plp/Technical_ACTION_SearchDocument_Thumbnail_BS_Mist.png
126+ :alt: Abstract illustration showing CRUD and aggregation operations
127+
128+ .. tab:: Administer and Manage
129+ :tabid: administer
130+
131+ .. procedure::
132+
133+ .. step:: Manage Databases and Collections
134+
135+ View information about databases, create collections or views,
136+ or drop databases - all from your shell. See all
137+ :ref:`Database Methods <mongosh-database-methods>`.
138+
139+ Perform collection operations, create or delete indexes, or
140+ explain queries with :ref:`Collection Methods
141+ <mongosh-collection-methods>`.
142+
143+ .. step:: Administer Servers
144+
145+ Manage :ref:`replication <mongosh-replication-methods>` or
146+ :ref:`sharding <mongosh-sharding-methods>` conveniently in
147+ your shell.
148+
149+ Check server status with a variety of :ref:`Server Status Methods
150+ <mongosh-server-status-methods>`.
151+
152+ .. step:: Manage Users and Roles
153+
154+ Create or update roles, define and update privileges, or drop
155+ roles using :ref:`Role Management Methods
156+ <mongosh-role-management-methods>`.
157+
158+ Create and update users, authenticate users, and manage user
159+ roles with :ref:`User Management Methods
160+ <mongosh-user-management-methods>`.
161+
162+ .. image:: /images/mongosh-plp/Technical_SOFTWARE_Terminal(1)_Spot.png
163+ :alt: Abstract illustration showing a terminal performing operations
164+
165+ .. tab:: Automate with Scripts
166+ :tabid: scripts
167+
168+ .. procedure::
169+
170+ .. step:: Run Scripts for CRUD or Administrative Tasks
171+
172+ Write scripts to run with the |mdb-shell| that perform CRUD or
173+ administrative operations in MongoDB.
174+
175+ For example, if you have a JS file that seeds synthetic or
176+ mock data into MongoDB in your development or staging
177+ environment, run the file with:
178+
179+ .. code-block:: sh
180+
181+ mongosh YOUR_JS_FILENAME.js
182+
183+ Explore a tutorial that uses the |mdb-shell| with JavaScript to
184+ access MongoDB: :ref:`mdb-shell-write-scripts`.
10185
11- .. default-domain :: mongodb
186+ .. step :: Create Custom Helper Functions with .mongoshrc
12187
13- .. contents:: On this page
14- :local:
15- :backlinks: none
16- :depth: 1
17- :class: singlecol
188+ Repeatedly writing large helper functions in the Shell? Store
189+ them in a :ref:`.mongoshrc config file <mongoshrc-js>`. For
190+ example, if you often find yourself converting date strings to
191+ ISO format for queries, create a function in ``.mongoshrc`` to
192+ handle it:
18193
19- The |mdb-shell|, ``mongosh``, is a fully functional JavaScript and
20- Node.js 16.x |repl| environment for interacting with MongoDB
21- deployments. You can use the |mdb-shell| to test queries and operations
22- directly with your database.
194+ .. code-block:: JavaScript
23195
24- :binary:`~bin.mongosh` is available as a standalone package in the
25- `MongoDB Download Center
26- <https://www.mongodb.com/try/download/shell?jmp=docs>`__.
196+ function toISO(dateString) {
197+ return new Date(dateString).toISOString();
198+ }
27199
28- Download and Install ``mongosh``
29- --------------------------------
200+ Then, call the function in ``mongosh``:
30201
31- To learn how to download and install the ``mongosh`` binary, see
32- :ref:`mdb-shell-install`.
202+ .. code-block:: sh
33203
34- Connect to a MongoDB Deployment
35- -------------------------------
204+ db.clientConnections.find( { connectTime: toISO("06/07/2017") } )
36205
37- Once you have installed the |mdb-shell| and added it to your system
38- ``PATH``, you can connect to a MongoDB deployment. To learn more, see
39- :ref:`mdb-shell-connect`.
206+ For more information, refer to :ref:`mongosh-write-scripts-config-file`.
40207
41- The |mdb-shell| versus the Legacy ``mongo`` Shell
42- -------------------------------------------------
208+ .. step:: Use or Publish Snippets
43209
44- The legacy ``mongo`` shell was deprecated in MongoDB 5.0 and removed in
45- MongoDB 6.0. The new MongoDB Shell, ``mongosh``, offers numerous
46- advantages over the legacy shell. The new shell has improved:
210+ :ref:`Pull existing snippets <snip-install-packages>` into
211+ your codebase for convenient reuse. Or :ref:`create and share
212+ snippets <snip-create>` for your custom use case.
47213
48- - compatibility with the MongoDB `Node.js
49- <https://www.mongodb.com/docs/drivers/node/current/>`__ driver
50- - syntax highlighting
51- - command history
52- - logging
214+ For example, you might have a snippet that validates the data
215+ you import daily as a cron job. You can publish this snippet,
216+ so your development team can access it. Publish to a community
217+ registry or configure a private registry.
218+
219+ For more information, refer to :ref:`snip-reg-config`.
53220
54- Some legacy methods are unavailable or have been replaced with updated
55- methods in ``mongosh``. To maintain backwards compatibility, the legacy
56- methods that ``mongosh`` supports use the same syntax as the
57- corresponding methods in the ``mongo`` shell.
221+ .. image:: /images/mongosh-plp/Technical_SOFTWARE_Terminal(4)_Spot.png
222+ :alt: Abstract illustration implying a terminal executing scripts
58223
59- To see the complete list of methods supported by ``mongosh``, refer to:
60- :doc:`MongoDB Shell Methods </reference/methods>`.
224+ .. kicker:: Learn More
61225
62- Learn More
63- ----------
226+ Other Powerful Features
227+ -----------------------
64228
65- - :ref:`Connect to a MongoDB Deployment <mdb-shell-connect>`
229+ .. card-group::
230+ :columns: 3
231+ :style: compact
66232
67- - :ref:`Perform CRUD Operations <mdb-shell-crud>`
233+ .. card::
234+ :cta: Unlock the Power of Editor Mode
235+ :url: https://www.mongodb.com/docs/mongodb-shell/reference/editor-mode/
236+ :icon: /images/mongosh-plp/icons/Technical_REALM_CLI10x.png
237+ :icon-alt: CLI Icon
68238
69- - :ref:`Run Aggregation Pipelines <mdb-shell-aggregation>`
239+ Use an external or built-in editor to work with multiline
240+ functions. Go beyond the line-oriented ``mongosh``
241+ default console.
70242
71- - :ref:`Use Editor Mode <mongosh-editor-mode>`.
243+ .. card::
244+ :cta: Access Session Logs
245+ :url: https://www.mongodb.com/docs/mongodb-shell/logs/
246+ :icon: /images/mongosh-plp/icons/Technical_MDB_Shell10x.png
247+ :icon-alt: MongoDB Shell Icon
72248
73- - :ref:`Write Scripts <mdb-shell-write-scripts>`
249+ Access session logs for any session within the last 30 days. Find
250+ the command syntax you can't quite remember, or look for common
251+ commands you can script.
74252
75- - :ref:`Retrieve Logs <mdb-shell-logs>`
253+ .. card::
254+ :cta: View Available Methods
255+ :url: https://www.mongodb.com/docs/mongodb-shell/reference/methods/
256+ :icon: /images/mongosh-plp/icons/General_ACTION_Develop10x.png
257+ :icon-alt: Terminal Icon in a circle
76258
77- - :ref:`View Available Methods in the MongoDB Shell <mdb-shell-methods>`
259+ Find out which methods ``mongosh`` supports. Get example
260+ syntax and parameter details for supported methods.
78261
79262.. toctree::
80263 :titlesonly:
@@ -91,4 +274,3 @@ Learn More
91274 /reference
92275 /changelog
93276 /reference/access-mdb-shell-help.txt
94-
0 commit comments