Welcome to MongoDB Shell (mongosh) - mongosh - MongoDB Docs (original) (raw)
The MongoDB Shell, mongosh
, is a JavaScript and Node.js REPLenvironment for interacting with MongoDB deployments inAtlas , locally, or on another remote host. Use the MongoDB Shell to test queries and interact with the data in your MongoDB database.
What You Can Do
Find your connection string. The connection string varies depending on the type of deployment you're connecting to.
Learn how to find your connection string forAtlas.
Or connect to aself-hosteddeployment.
Connect to a MongoDB deployment using the connection string.
The following connection string connects to an Atlas deployment:
mongosh "mongodb+srv://mycluster.abcd1.mongodb.net/myFirstDatabase" --apiVersion 1 --username <username>
Use your chosen connection type to view your data, import documents, and run queries.
For more information, refer to Perform CRUD Operations.
View information about databases, create collections or views, or drop databases - all from your shell. See allDatabase Methods.
Perform collection operations, create or delete indexes, or explain queries with Collection Methods.
Write scripts to run with the MongoDB Shell that perform CRUD or administrative operations in MongoDB.
For example, if you have a JS file that seeds synthetic or mock data into MongoDB in your development or staging environment, run the file with:
mongosh YOUR_JS_FILENAME.js
Explore a tutorial that uses the MongoDB Shell with JavaScript to access MongoDB: Write Scripts.
Repeatedly writing large helper functions in the Shell? Store them in a .mongoshrc config file. For example, if you often find yourself converting date strings to ISO format for queries, create a function in .mongoshrc
to handle it:
function toISO(dateString) {
return new Date(dateString).toISOString();
}
Then, call the function in mongosh
:
db.clientConnections.find( { connectTime: toISO("06/07/2017") } )
For more information, refer to Execute Code From a Configuration File.
Learn More
Access session logs for any session within the last 30 days. Find the command syntax you can't quite remember, or look for common commands you can script.
Find out which methods mongosh
supports. Get example syntax and parameter details for supported methods.