CodingNic

Introduction to MongoDB and the Node.js Driver

Basic CRUD with the Driver

Introduction to MongoDB and the Node.js Driver 20 min read

Basic CRUD with the Driver

Objectives

By the end of this lesson, you should be able to:

  • Insert one or many documents into a collection
  • Query documents with find, findOne, and a filter object
  • Update and delete documents with the native driver

💡 Why this matters: This is MongoDB’s version of Modules 1 and 2, the CRUD basics everything else in the MongoDB half of this course builds on.

⚠️ A note on verification: as in the last lesson, this course’s own tooling has no route to a local MongoDB server. The code and output below reflect the official mongodb driver’s stable, current, documented API. Try it yourself, on your own machine, against a real MongoDB server.

Inserting Documents

javascript
const db = client.db('school');
const students = db.collection('students');

const result = await students.insertOne({ name: 'Erin', grade: 9 });
console.log(result);
text
{
  acknowledged: true,
  insertedId: ObjectId('65f1a2b3c4d5e6f7a8b9c0d1')
}

insertOne returns an insertedId, MongoDB generates a unique ObjectId automatically, the equivalent of SERIAL’s auto-incrementing id in PostgreSQL, except it’s a longer, globally unique value rather than a simple counter.

Inserting several documents at once:

javascript
const result = await students.insertMany([
  { name: 'Jordan', grade: 10 },
  { name: 'Maya', grade: 9 }
]);
console.log(result.insertedCount);
text
2

Reading Documents

find returns every matching document, as a cursor, not an array directly:

javascript
const cursor = students.find({});
const all = await cursor.toArray();
console.log(all);
text
[
  { _id: ObjectId('...'), name: 'Erin', grade: 9 },
  { _id: ObjectId('...'), name: 'Jordan', grade: 10 },
  { _id: ObjectId('...'), name: 'Maya', grade: 9 }
]

{} as the filter matches every document, toArray() pulls the full result set into a normal array. Filtering works with a plain object, matched field by field:

javascript
const ninthGraders = await students.find({ grade: 9 }).toArray();
console.log(ninthGraders);
text
[
  { _id: ObjectId('...'), name: 'Erin', grade: 9 },
  { _id: ObjectId('...'), name: 'Maya', grade: 9 }
]

findOne returns a single matching document directly (not a cursor), or null if nothing matches:

javascript
const jordan = await students.findOne({ name: 'Jordan' });
console.log(jordan);
text
{ _id: ObjectId('...'), name: 'Jordan', grade: 10 }

Updating Documents

javascript
const result = await students.updateOne(
  { name: 'Erin' },
  { $set: { grade: 10 } }
);
console.log(result);
text
{
  acknowledged: true,
  matchedCount: 1,
  modifiedCount: 1
}

The first argument is the filter (which document to update), the second describes the change, $set updates specific fields without touching the rest of the document. Unlike SQL’s UPDATE, MongoDB doesn’t overwrite the whole document by default, $set is what makes it a partial update.

updateMany applies the same change to every matching document:

javascript
const result = await students.updateMany(
  { grade: 9 },
  { $set: { status: 'active' } }
);
console.log(result.modifiedCount);
text
1

Deleting Documents

javascript
const result = await students.deleteOne({ name: 'Jordan' });
console.log(result);
text
{ acknowledged: true, deletedCount: 1 }

deleteMany removes every matching document, exactly like deleteOne but not stopping after the first match.

Try It

  1. Insert three documents into a students collection using insertOne and insertMany.
  2. Query all of them with find({}).toArray(), and a filtered subset with find({ grade: 9 }).toArray().
  3. Update one document’s field with updateOne and $set, and confirm modifiedCount is 1.
  4. Delete one document with deleteOne, then confirm it’s gone with another find.

Recap

  • insertOne/insertMany add documents, each gets an auto-generated _id unless one is provided.
  • find returns a cursor (call .toArray() for a plain array), findOne returns a single document or null.
  • updateOne/updateMany take a filter and a $set (or other update operator), deleteOne/deleteMany take a filter alone.

This is the final lesson of this module before exercises. Next module: Mongoose, schemas and validation on top of everything covered here.