Master MongoDB CRUD Operations: 4 Essential Basic Operations for Beginners
This article introduces the basic CRUD operations of MongoDB. MongoDB is a document - oriented database, where data is stored in BSON format and documents are placed in collections without a fixed table structure. Before performing operations, the service needs to be started. Then, enter the Shell through `mongo`, use `use` to switch databases, and select a collection with `db.collection name`. **Create**: To insert a single document, use `insertOne()` (e.g., inserting a user document). For multiple documents, use `insertMany()` (e.g., inserting multiple users). The return result contains the document ID and operation confirmation information. **Read**: The core is `find()`, which supports conditional filtering (e.g., `age: { $gt: 20 }`), field projection (`{name:1, _id:0}`), sorting (`sort({age:1})`), and limiting the number of results (`limit(2)`). **Update**: `updateOne()` is used to update a single document, and `updateMany()` is used to update multiple documents. Use `$set` to overwrite fields (e.g., changing the name) and `$inc` to increment fields (e.g., increasing the age by 1). **Delete**: `deleteOne()` is for deleting a single document, `deleteMany()` is for deleting multiple documents, and `deleteMany({})` is used to clear the collection. The operation needs to
Read More