mongodb 7.5.0Getting Started with MongoDB
Learn MongoDB's document model, create a tutorial database, and run your first query with the official Node.js driver inside MongoG.
What you will learn
MongoDB stores records as flexible BSON documents instead of rows in a fixed table. In this first tutorial you will select a database, create a collection by writing its first documents, and read those documents back in MongoG.
You need a running MongoDB deployment and a connection profile in MongoG. A local Community server and MongoDB Atlas both work. MongoG manages the connection, so tutorial scripts never need to contain a connection string or password.
Databases, collections, and documents
A MongoDB deployment contains databases. A database contains collections, and a collection contains documents. A document is a set of fields represented with JavaScript-like object syntax:
const learner = {
name: "Ada Lovelace",
role: "engineer",
active: true,
joinedAt: ISODate("2026-01-15T09:00:00Z")
};
printjson(learner);
MongoDB adds a unique _id field when you do not provide one. The value is commonly an ObjectId, although applications can choose another unique value.
Run your first MongoG script
Open a query tab for your connection and run this script. use() changes the active database for the tab's script context. The collection is created automatically when the first insert succeeds.
use("mongog_tutorial");
const learners = db.collection("learners");
learners.deleteMany({ tutorial: "getting-started" });
learners.insertMany([
{ name: "Ada", track: "queries", active: true, tutorial: "getting-started" },
{ name: "Grace", track: "aggregation", active: true, tutorial: "getting-started" },
]);
learners.find({ tutorial: "getting-started" }).sort({ name: 1 });
MongoG automatically waits for each database operation. The final find() returns a real driver cursor; MongoG reads an initial page and keeps the cursor available for result paging instead of calling toArray() behind your back.
Expected result
The results panel shows two documents ordered by name. Each document has the fields you supplied plus an _id. Re-running the script remains predictable because the first delete removes only documents created by this tutorial.
Change and verify a document
Use a filter to choose one document and $set to change selected fields:
const learners = db.collection("learners");
learners.updateOne(
{ name: "Ada", tutorial: "getting-started" },
{ $set: { active: false, completedAt: ISODate() } },
);
learners.findOne({ name: "Ada", tutorial: "getting-started" });
MongoDB updates only the fields named under $set. The rest of the document remains intact. This targeted update is different from replacing an entire document.
Read the driver result, not only the data
Write methods return result objects that help a script verify what happened. A successful command can still match zero documents, so inspect counts when correctness depends on a record being changed:
const update = await learners.updateOne(
{ name: "Grace", tutorial: "getting-started" },
{ $set: { track: "indexes" } },
);
printjson({
acknowledged: update.acknowledged,
matched: update.matchedCount,
modified: update.modifiedCount,
});
if (update.matchedCount !== 1) {
throw new Error("Expected exactly one tutorial learner.");
}
acknowledged describes the configured write concern response. matchedCount describes how many documents matched the filter, while modifiedCount describes how many stored values changed. If Grace already had the indexes track, the write can match one document and modify zero.
Understand when data becomes durable
Selecting a database with use() and obtaining a collection object are client-side choices. They do not create persisted server data. The database and collection normally appear after the first successful write.
MongoDB sends reads and writes through the connected deployment topology. A standalone server is enough for these lessons; transactions and change streams later require a replica set or sharded cluster. Atlas clusters already use supported replicated topologies.
For a production application, the database user should receive only the roles it needs. A tutorial administrator account is convenient locally but is a poor default for routine browsing or application traffic.
Practice the complete CRUD loop
Use a new tutorial tag such as getting-started-practice and complete this sequence:
- Insert three learners with different tracks.
- Query only active learners and sort them by name.
- Update one learner by a precise name-and-tag filter.
- Count the remaining documents.
- Delete only your three practice documents.
- Run the count again and confirm it is zero.
Keep each write separate from its verification query. This makes it obvious which result belongs to which operation in MongoG’s statement results.
Common mistakes
- Do not paste a credentialed connection URI into a query script. Create a MongoG connection profile and keep credentials in the local encrypted vault.
- Do not assume a collection must be declared before use. MongoDB normally creates it on the first successful write, although explicit creation is available when you need collection options.
- Do not treat
_idas a display-only field. It is the document's unique primary key and is indexed automatically. - Do not use the tutorial cleanup filter against your application data. Keep tutorial work in the dedicated
mongog_tutorialdatabase. - Do not assume an acknowledged update changed a record. Check matched and modified counts when the script expects a specific outcome.
Continue learning
Next, learn how documents, collections, and BSON represent application data. For server installation and Atlas setup, use the official MongoDB get started guides.