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 obj: application
--- ---
#wip #🐇 #notnow # MongoDB
MongoDB is a popular NoSQL database that is document-oriented and designed for scalability and flexibility 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 ## Docker-Compose
```yml ```yml
@ -12,13 +12,12 @@ version: '3'
services: services:
mongo: mongo:
image: mongo image: mongo
container_name: mongo
restart: always restart: always
environment: environment:
MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password MONGO_INITDB_ROOT_PASSWORD: password
volumes: volumes:
- mongodb_data:/data/db - ./db:/data/db
ports: ports:
- "27017:27017" - "27017:27017"
``` ```
@ -63,23 +62,29 @@ To delete multiple documents: `db.<collection>.deleteMany(<filter>)`
To delete all documents in a collection: `db.<collection>.deleteMany({})` To delete all documents in a collection: `db.<collection>.deleteMany({})`
### Filters ### Filters
- $eq: The $eq operator matches documents where the value of a field equals a specified value. Usage:
- $ne: The $ne operator matches documents where the value of a field is not equal to a specified value. ```json
- $gt: The $gt operator matches documents where the value of a field is greater than a specified value. { "field": { "$mod": "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. - `$eq`: The `$eq` operator matches documents where the value of a field equals a specified value.
- $in: The $in operator matches documents where the value of a field equals any value in a specified array. - `$ne`: The `$ne` operator matches documents where the value of a field is not equal to a specified value.
- $nin: The $nin operator matches documents where the value of a field does not equal any value in a specified array. - `$gt`: The `$gt` operator matches documents where the value of a field is greater than a specified value.
- $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. - `$gte`: The `$gte` operator matches documents where the value of a field is greater than or equal to a specified value.
- $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. - `$lt`: The `$lt` operator matches documents where the value of a field is less than a specified value.
- $not: The $not operator performs a logical NOT operation on the specified expression and selects the documents that do not match the expression. - `$lte`: The `$lte` operator matches documents where the value of a field is less than or equal to a specified value.
- $exists: The $exists operator matches documents where a specified field exists or does not exist. - `$in`: The `$in` operator matches documents where the value of a field equals any value in a specified array.
- $type: The $type operator matches documents where a specified field has a specific BSON type. - `$nin`: The `$nin` operator matches documents where the value of a field does not equal any value in a specified array.
- $regex: The $regex operator matches documents where a specified field matches a regular expression. - `$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.
- $text: The $text operator performs a text search on the specified field(s). - `$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.
- $elemMatch: The $elemMatch operator matches documents where a specified array field contains at least one element that matches all the specified conditions. - `$not`: The `$not` operator performs a logical NOT operation on the specified expression and selects the documents that do not match the expression.
- $size: The $size operator matches documents where a specified array field has a specific size. - `$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:** **Example:**
```js ```js
@ -94,20 +99,19 @@ db.users.find(
``` ```
### Update Modifiers ### Update Modifiers
- $set: The $set operator updates the value of a field in a document. - `$set`: The `$set` operator updates the value of a field in a document.
- $unset: The $unset operator removes a field from 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. - `$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. - `$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. - `$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. - `$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. - `$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. - `$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. - `$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. - `$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. - `$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. - `$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. - `$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
**Example:** **Example:**
```js ```js