On this page
CRUD Operations
Create
Inserting Documents
-
insertOne(document)
- Inserts a single document into a collection.
- Example:
-
insertMany(documents)
- Inserts multiple documents into a collection.
- Example:
Read
Querying Documents
-
find(query)
- Retrieves multiple documents that match the query.
- Example:
-
findOne(query)
- Retrieves a single document that matches the query.
- Example:
Query Operators
-
$eq
(Equal)- Matches values that are equal to a specified value.
- Example:
-
$gt
(Greater Than)- Matches values that are greater than a specified value.
- Example:
-
$lt
(Less Than)- Matches values that are less than a specified value.
- Example:
Projection (Selecting Specific Fields)
- Projection allows you to specify which fields to include or exclude in the results.
- Example:
- The above command retrieves only the
name
andcity
fields.
Update
Updating Documents
-
updateOne(filter, update)
- Updates a single document that matches the filter.
- Example:
-
updateMany(filter, update)
- Updates multiple documents that match the filter.
- Example:
Update Operators
-
$set
- Sets the value of a field in a document.
- Example:
-
$unset
- Removes a field from a document.
- Example:
-
$inc
- Increments the value of a field by a specified amount.
- Example:
Delete
Deleting Documents
-
deleteOne(filter)
- Deletes a single document that matches the filter.
- Example:
-
deleteMany(filter)
- Deletes multiple documents that match the filter.
- Example: