This commit is contained in:
JMARyA 2024-01-16 09:35:22 +01:00
parent 7abeac09d0
commit 00892fb1d4
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -3,8 +3,8 @@ website: https://www.mongodb.com
obj: application
---
#wip #🐇 #notnow
MongoDB is a popular NoSQL database that is document-oriented and designed for scalability and flexibility
# MongoDB
MongoDB is a popular NoSQL database that is document-oriented and designed for scalability and flexibility. You can work with MongoDB in a GUI with [MongoDB Compass](MongoDB%20Compass.md).
## Docker-Compose
```yml
@ -12,13 +12,12 @@ version: '3'
services:
mongo:
image: mongo
container_name: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
volumes:
- mongodb_data:/data/db
- ./db:/data/db
ports:
- "27017:27017"
```
@ -63,23 +62,29 @@ To delete multiple documents: `db.<collection>.deleteMany(<filter>)`
To delete all documents in a collection: `db.<collection>.deleteMany({})`
### Filters
- $eq: The $eq operator matches documents where the value of a field equals a specified value.
- $ne: The $ne operator matches documents where the value of a field is not equal to a specified value.
- $gt: The $gt operator matches documents where the value of a field is greater than a specified value.
- $gte: The $gte operator matches documents where the value of a field is greater than or equal to a specified value.
- $lt: The $lt operator matches documents where the value of a field is less than a specified value.
- $lte: The $lte operator matches documents where the value of a field is less than or equal to a specified value.
- $in: The $in operator matches documents where the value of a field equals any value in a specified array.
- $nin: The $nin operator matches documents where the value of a field does not equal any value in a specified array.
- $and: The $and operator performs a logical AND operation on an array of two or more expressions and selects the documents that satisfy all the expressions.
- $or: The $or operator performs a logical OR operation on an array of two or more expressions and selects the documents that satisfy at least one of the expressions.
- $not: The $not operator performs a logical NOT operation on the specified expression and selects the documents that do not match the expression.
- $exists: The $exists operator matches documents where a specified field exists or does not exist.
- $type: The $type operator matches documents where a specified field has a specific BSON type.
- $regex: The $regex operator matches documents where a specified field matches a regular expression.
- $text: The $text operator performs a text search on the specified field(s).
- $elemMatch: The $elemMatch operator matches documents where a specified array field contains at least one element that matches all the specified conditions.
- $size: The $size operator matches documents where a specified array field has a specific size.
Usage:
```json
{ "field": { "$mod": "value" } }
```
- `$eq`: The `$eq` operator matches documents where the value of a field equals a specified value.
- `$ne`: The `$ne` operator matches documents where the value of a field is not equal to a specified value.
- `$gt`: The `$gt` operator matches documents where the value of a field is greater than a specified value.
- `$gte`: The `$gte` operator matches documents where the value of a field is greater than or equal to a specified value.
- `$lt`: The `$lt` operator matches documents where the value of a field is less than a specified value.
- `$lte`: The `$lte` operator matches documents where the value of a field is less than or equal to a specified value.
- `$in`: The `$in` operator matches documents where the value of a field equals any value in a specified array.
- `$nin`: The `$nin` operator matches documents where the value of a field does not equal any value in a specified array.
- `$and`: The `$and` operator performs a logical AND operation on an array of two or more expressions and selects the documents that satisfy all the expressions.
- `$or`: The `$or` operator performs a logical OR operation on an array of two or more expressions and selects the documents that satisfy at least one of the expressions.
- `$not`: The `$not` operator performs a logical NOT operation on the specified expression and selects the documents that do not match the expression.
- `$exists`: The `$exists` operator matches documents where a specified field exists or does not exist.
- `$type`: The `$type` operator matches documents where a specified field has a specific [BSON](../files/BSON.md) type.
- `$regex`: The `$regex` operator matches documents where a specified field matches a [regular expression](../tools/Regex.md).
- `$text`: The `$text` operator performs a text search on the specified field(s).
- `$elemMatch`: The `$elemMatch` operator matches documents where a specified array field contains at least one element that matches all the specified conditions.
- `$size`: The `$size` operator matches documents where a specified array field has a specific size.
- `$exists`: The `$exists` operator matches documents that contain or do not contain a specified field, including documents where the field value is `null`.
**Example:**
```js
@ -94,20 +99,19 @@ db.users.find(
```
### Update Modifiers
- $set: The $set operator updates the value of a field in a document.
- $unset: The $unset operator removes a field from a document.
- $inc: The $inc operator increments the value of a field in a document.
- $mul: The $mul operator multiplies the value of a field in a document.
- $min: The $min operator updates the value of a field in a document if the new value is lower than the existing value.
- $max: The $max operator updates the value of a field in a document if the new value is higher than the existing value.
- $rename: The $rename operator renames a field in a document.
- $addToSet: The $addToSet operator adds a value to an array field in a document if the value does not already exist in the array.
- $push: The $push operator appends a value to an array field in a document.
- $pull: The $pull operator removes a value from an array field in a document.
- $currentDate: The $currentDate operator sets the value of a field in a document to the current date and time.
- $each: The $each operator can be used with $addToSet and $push to append multiple values to an array field in a document.
- $sort: The $sort operator can be used with $push to sort the elements in an array field in a document.
- $addToSet: Add an element to an array
- `$set`: The `$set` operator updates the value of a field in a document.
- `$unset`: The `$unset` operator removes a field from a document.
- `$inc`: The `$inc` operator increments the value of a field in a document.
- `$mul`: The `$mul` operator multiplies the value of a field in a document.
- `$min`: The `$min` operator updates the value of a field in a document if the new value is lower than the existing value.
- `$max`: The `$max` operator updates the value of a field in a document if the new value is higher than the existing value.
- `$rename`: The `$rename` operator renames a field in a document.
- `$addToSet`: The `$addToSet` operator adds a value to an array field in a document if the value does not already exist in the array.
- `$push`: The `$push` operator appends a value to an array field in a document.
- `$pull`: The `$pull` operator removes a value from an array field in a document.
- `$currentDate`: The `$currentDate` operator sets the value of a field in a document to the current date and time.
- `$each`: The `$each`¥ operator can be used with `$addToSet` and `$push` to append multiple values to an array field in a document.
- `$sort`: The `$sort` operator can be used with `$push` to sort the elements in an array field in a document.
**Example:**
```js