MongoDB Tutorial
Store and query flexible documents with the MongoDB database.
Getting started6 lessons
- 1MongoDB IntroductionMongoDB is a document database.
- 2MongoDB Get StartedInstall MongoDB Community or create a free Atlas cluster in the cloud, then connect with m.
- 3Databases, Collections and DocumentsA database holds collections, a collection holds documents.
- 4BSON Data TypesDocuments are stored as BSON, which adds types to JSON: string, int, long, double, decimal.
- 5ObjectId and _idEvery document has a unique _id.
- 6Atlas, Compass and DriversMongoDB Atlas is the hosted cloud service.
Insert and read7 lessons
- 7Insert One DocumentinsertOne adds a single document and returns its _id.
- 8Insert Many DocumentsinsertMany adds an array of documents in one call.
- 9Find Documentsfind returns a cursor over matching documents.
- 10ProjectionThe second argument chooses which fields to return.
- 11Sort, Limit and Skipsort orders results, 1 ascending and -1 descending.
- 12Counting DocumentscountDocuments counts documents that match a filter.
- 13Cursors and Batchesfind returns a cursor that fetches documents in batches.
Query operators7 lessons
- 14Comparison Operators$eq, $ne, $gt, $gte, $lt, $lte, $in and $nin compare values.
- 15Logical Operators$and, $or, $nor and $not combine conditions.
- 16Element and Type Operators$exists tests whether a field is present and $type checks its BSON type.
- 17Text Matching with Regex$regex matches text patterns.
- 18Querying ArraysA filter on an array field matches if any element matches.
- 19Querying Nested DocumentsUse dot notation to reach into embedded documents and arrays by position.
- 20$expr and Computed Queries$expr lets a query compare two fields of the same document or use aggregation expressions.
Update and delete7 lessons
- 21Update DocumentsupdateOne and updateMany change documents with update operators such as $set.
- 22More Update Operators$inc adds to numbers, $unset removes fields, $rename renames, $min and $max keep the small.
- 23Updating Arrays$push adds an item, $addToSet adds if missing, $pull removes matches, $pop removes the fir.
- 24Upsert and ReplaceWith upsert: true, update inserts a document when nothing matches.
- 25findOneAndUpdatefindOneAndUpdate changes a document and returns it in one atomic step.
- 26Delete DocumentsdeleteOne removes the first match and deleteMany removes all matches.
- 27Bulk WritesbulkWrite sends many different operations in one round trip, which is much faster than sep.
Data modeling14 lessons
- 28Embedding versus ReferencingEmbed data that is read together and has a bounded size, such as an address.
- 29One to Many PatternsFew children: embed an array.
- 30Schema ValidationAdd a $jsonSchema validator to a collection so documents must match rules for types and re.
- 31IndexesAn index lets MongoDB find documents without scanning the collection.
- 32Explain Plansexplain shows how a query runs.
- 33Text and TTL IndexesA text index supports $text search.
- 34Embedding DocumentsStore related data inside one document.
- 35Referencing DocumentsStore the _id of another document.
- 36One to Many ModelingEmbed few, reference many.
- 37Schema Design RulesModel data how it is queried.
- 38Schema Validation$jsonSchema enforces shape.
- 39Capped CollectionsFixed size collections that overwrite oldest data.
- 40TTL IndexesExpire documents automatically.
- 41Working with DatesUse Date objects for time queries.
Aggregation7 lessons
- 42Aggregation Pipelineaggregate runs documents through stages such as $match, $group, $sort and $project.
- 43$match, $project and $addFields$match filters early, $project chooses or renames fields and $addFields adds computed fiel.
- 44$group AccumulatorsAccumulators include $sum, $avg, $min, $max, $first, $last, $push and $addToSet.
- 45$unwind$unwind turns an array into one document per element so you can group or count items.
- 46$lookup: Joins$lookup joins another collection like a left outer join.
- 47$facet, $bucket and $count$bucket groups by ranges, $facet runs several pipelines on the same input at once and $cou.
- 48$out and $merge$out writes pipeline results to a new collection and $merge updates or inserts into an exi.
Advanced MongoDBPremium8 lessons
- 49TransactionsMulti document transactions give ACID guarantees across documents and collections on repli.
- 50Replica SetsA replica set copies data across servers for availability.
- 51ShardingSharding spreads data across machines by a shard key.
- 52Read and Write ConcernWrite concern sets how many servers must confirm a write, such as w: "majority".
- 53Change StreamsChange streams let your app subscribe to inserts, updates and deletes in real time on repl.
- 54Atlas Search and Vector SearchAtlas offers full text search and vector search inside MongoDB with the $search and $vecto.
- 55Time Series CollectionsTime series collections store measurements efficiently with a time field and optional meta.
- 56GridFS and Large FilesDocuments are limited to 16 MB.
MongoDB in practicePremium10 lessons
- 57Security ChecklistTurn on authentication, create users with least privilege roles, use TLS, restrict network.
- 58Backup and Restoremongodump exports data and mongorestore imports it.
- 59Import and Export Datamongoimport loads JSON or CSV files and mongoexport writes them.
- 60MongoDB with Node.js and MongooseMongoose adds schemas, validation and models on top of the Node driver.
- 61MongoDB with PythonPyMongo is the official Python driver.
- 62Monitoring and PerformanceWatch slow queries with the profiler, check index use with explain, monitor memory, connec.
- 63Schema Design PatternsCommon patterns: bucket for time data, computed for stored totals, subset for hot fields, .
- 64Migrating from SQLMap tables to collections, rows to documents and joins to embedding or $lookup.
- 65MongoDB Best PracticesDesign for your queries, index what you filter and sort, avoid unbounded arrays, use proje.
- 66Next StepsYou can now model data, query, update and aggregate with MongoDB.
MongoDB basics16 lessons
- 67What is MongoDBA document database that stores JSON like documents in collections.
- 68Databases, Collections, DocumentsA database holds collections and a collection holds documents.
- 69Using mongoshmongosh is the interactive shell.
- 70BSON TypesMongoDB stores BSON with types like String, Int32, Double, Date and ObjectId.
- 71ObjectId_id defaults to a 12 byte ObjectId.
- 72insertOneAdd one document.
- 73insertManyAdd several documents in one call.
- 74findRead documents.
- 75ProjectionChoose fields to return.
- 76Comparison Operators$eq, $ne, $gt, $gte, $lt, $lte, $in and $nin.
- 77Logical Operators$and, $or, $nor and $not combine conditions.
- 78$exists and $typeCheck field presence and type.
- 79Regular ExpressionsMatch text with $regex.
- 80sort, limit and skipOrder and page results.
- 81CountingcountDocuments counts matches.
- 82distinctList unique values of a field.
Updating and deleting11 lessons
- 83updateOne and $setChange fields in one document.
- 84updateManyChange many documents.
- 85$inc and $mulChange numbers in place.
- 86$unset and $renameRemove or rename fields.
- 87Array Updates$push adds, $pull removes and $addToSet adds without duplicates.
- 88Positional UpdateThe $ operator updates the matched array item.
- 89UpsertInsert when nothing matches.
- 90replaceOneReplace the whole document.
- 91findOneAndUpdateUpdate and return the document.
- 92Deleting DocumentsdeleteOne and deleteMany remove documents.
- 93Dropping Collectionsdrop removes a collection and its documents.
Indexes and aggregation12 lessons
- 94Creating IndexesIndexes speed up queries.
- 95Unique IndexesPrevent duplicate values.
- 96Compound IndexesIndex several fields in order.
- 97explainSee how a query runs.
- 98Text SearchText indexes support $text.
- 99Aggregation PipelineStages transform documents in order.
- 100$groupGroup and total values.
- 101$project and $addFieldsReshape documents.
- 102$lookupA left outer join.
- 103$unwindFlatten arrays into documents.
- 104$sort, $limit, $skipOrder and slice in a pipeline.
- 105$count and $facetCount and run sub pipelines.
MongoDB in applications16 lessons
- 106Using the Node DriverConnect and query from Node.js.
- 107Mongoose BasicsMongoose adds schemas and models.
- 108Connection StringsURIs describe how to connect.
- 109MongoDB AtlasAtlas is hosted MongoDB.
- 110Users and RolesCreate users with limited roles.
- 111Backup and Restoremongodump and mongorestore copy data.
- 112Import and Exportmongoimport and mongoexport move JSON or CSV.
- 113Transactions IntroMulti document transactions need a replica set.
- 114Replica SetsCopies of data across servers for availability.
- 115Sharding IntroSplit data across servers by a shard key.
- 116Project: Contacts BookPractice all CRUD operations.
- 117Project: Blog DataPosts with embedded comments.
- 118Project: Sales ReportGroup orders by customer.
- 119Security BasicsEnable auth, restrict network access and never expose the port.
- 120Performance TipsIndex filters and sorts, project only needed fields, avoid huge documents.
- 121MongoDB Best PracticesDesign for queries, add indexes, validate schemas and monitor.