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
mongodbdriver’s stable, current, documented API. Try it yourself, on your own machine, against a real MongoDB server.
Inserting Documents
const db = client.db('school');
const students = db.collection('students');
const result = await students.insertOne({ name: 'Erin', grade: 9 });
console.log(result);
{
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:
const result = await students.insertMany([
{ name: 'Jordan', grade: 10 },
{ name: 'Maya', grade: 9 }
]);
console.log(result.insertedCount);
2
Reading Documents
find returns every matching document, as a cursor, not an array directly:
const cursor = students.find({});
const all = await cursor.toArray();
console.log(all);
[
{ _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:
const ninthGraders = await students.find({ grade: 9 }).toArray();
console.log(ninthGraders);
[
{ _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:
const jordan = await students.findOne({ name: 'Jordan' });
console.log(jordan);
{ _id: ObjectId('...'), name: 'Jordan', grade: 10 }
Updating Documents
const result = await students.updateOne(
{ name: 'Erin' },
{ $set: { grade: 10 } }
);
console.log(result);
{
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:
const result = await students.updateMany(
{ grade: 9 },
{ $set: { status: 'active' } }
);
console.log(result.modifiedCount);
1
Deleting Documents
const result = await students.deleteOne({ name: 'Jordan' });
console.log(result);
{ acknowledged: true, deletedCount: 1 }
deleteMany removes every matching document, exactly like deleteOne but not stopping after the first match.
Try It
- Insert three documents into a
studentscollection usinginsertOneandinsertMany. - Query all of them with
find({}).toArray(), and a filtered subset withfind({ grade: 9 }).toArray(). - Update one document’s field with
updateOneand$set, and confirmmodifiedCountis1. - Delete one document with
deleteOne, then confirm it’s gone with anotherfind.
Recap
insertOne/insertManyadd documents, each gets an auto-generated_idunless one is provided.findreturns a cursor (call.toArray()for a plain array),findOnereturns a single document ornull.updateOne/updateManytake a filter and a$set(or other update operator),deleteOne/deleteManytake 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.