MongoDB CRUD Operations
MongoDB provides rich semantics for reading and manipulating data. CRUD stands for create, read, update, and delete. These terms are the foundation for all interactions with the database.
3.1 MongoDB CRUD Introduction
MongoDB stores data in the form of documents, which are JSON-like field and value pairs. Documents are analogous to structures in programming languages that associate keys with values (e.g. dictionaries, hashes, maps, and associative arrays). Formally, MongoDB documents are BSON documents. BSON is a binary representation of JSON with additional type information.
In the documents, the value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents.MongoDB stores all documents in collections. A collection is a group of related documents that have a set of shared common indexes. Collections are analogous to a table in relational databases.
3.2 MongoDB CRUD Concepts
The Read Operations and Write Operations documents introduce the behavior and operations ofread and write operations for MongoDB deployments.
- Read Operations – Queries are the core operations that return data in MongoDB. Introduces queries, their behavior, and performances.
- Cursors- Queries return iterable objects, called cursors, that hold the full result set.
- Query Optimization – Analyze and improve query performance.
- Distributed Queries – Describes how sharded clusters and replica sets affect the performance of read operations.
- Write Operations – Write operations insert, update, or remove documents in MongoDB. Introduces data create and modify operations, their behavior, and performances.
- Write Concern- Describes the kind of guarantee MongoDB provides when reporting on the success of a write operation.
- Distributed Write Operations – Describes how MongoDB directs write operations on sharded clusters and replica sets and the performance characteristics of these operations.
3.3 MongoDB CRUD Tutorials
The following tutorials provide instructions for querying and modifying data. For a higher-level overview of these operations, see MongoDB CRUD Operations
- Insert Documents – Insert new documents into a collection.
- Query Documents – Find documents in a collection using search criteria.
- Modify Documents – Modify documents in a collection
- Remove Documents – Remove documents from a collection.
- Limit Fields to Return from a Query – Limit which fields are returned by a query.
- Limit Number of Elements in an Array after an Update –Use $push with modifiers to sort and maintain an array of fixed size.
- Iterate a Cursor in the mongo Shell – Access documents returned by a find query by iterating the cursor, either manually or using the iterator index.
- Analyze Query Performance – Use query introspection to analyze the efficiency of queries and determine how a query uses available indexes.
- Perform Two Phase Commits – Use two-phase commits when writing data to multiple documents.
- Update Document if Current – Update a document only if it has not changed since it was last read.
- Create Tailable Cursor – Create tailable cursors for use in capped collections with high numbers of write operations for which an index would be too expensive.
- Create an Auto-Incrementing Sequence Field – Describes how to create an incrementing sequence number for the _id field using a Counters Collection or an Optimistic Loop.