Connecting to MongoDB and Exploring the Shell
After setting up a database environment, learn how to connect and inspect the deployment before writing application queries.
Connecting to a local server
mongosh "mongodb://127.0.0.1:27017"
If the connection succeeds, the shell connects to the local MongoDB server. If it fails, check whether the server is running, whether the port is correct, and whether authentication or network restrictions apply.
Connecting to a managed deployment
A MongoDB Atlas connection string commonly has a form similar to:
mongodb+srv://<username>:<password>@<cluster-host>/<database>?retryWrites=true&w=majority
This is a template, not a working connection string. Replace the placeholders with the values from your deployment and encode special characters in credentials as required by the connection-string format. Avoid committing credentials to source control.
Select a database
use learning
The shell switches its current database context to learning. The database may not be physically created until data is written or a collection is explicitly created.
Inspect the current database
db
The shell displays the current database name.
List collections
db.getCollectionNames()
This returns the names of collections in the current database that are visible to the connected user.
Insert your first document
db.courseDemo.insertOne({
title: "MongoDB Fundamentals",
level: "beginner",
published: true
});
This creates a collection if necessary and inserts a document. If you omit _id, MongoDB generates one.
Read the document
db.courseDemo.find();
The shell returns documents matching the query. With no filter, the query matches every document in the collection.
Understand the db variable
In mongosh, db represents the current database. Expressions such as db.users.find() access a collection through that database context.
The db variable is a shell interface, not a global database object that exists in every programming language. In Node.js, you interact with MongoDB through a driver or an object-document mapper such as Mongoose.
use learning
db.courseDemo.insertMany([
{
title: "MongoDB Fundamentals",
level: "beginner",
published: true
},
{
title: "MongoDB Querying",
level: "intermediate",
published: false
}
]);
// Display all course documents.
db.courseDemo.find();
// Display only published courses.
db.courseDemo.find({ published: true });
// Count the documents matching a filter.
db.courseDemo.countDocuments({ published: true });learning database, inserts two documents, reads the collection, filters published courses, and counts matching documents. The shell's use command is a shell helper. The database is persisted when data is written or a collection is created. countDocuments() counts documents matching the specified filter.- Use
mongoshto connect to a MongoDB deployment. - The
useshell command changes the current database context. - MongoDB can create a collection when the first document is inserted.
find()reads documents and accepts a filter.countDocuments()counts documents matching a filter.
What does `db.courseDemo.find({ published: true })` do?