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.
Docker-Compose
version: '3'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
volumes:
- ./db:/data/db
ports:
- "27017:27017"Usage
Connecting to MongoDB
To connect to MongoDB using the mongo shell:
mongo mongodb://<username>:<password>@<hostname>:<port>/<database>
Replace <username>, <password>, <hostname>, <port>, and <database> with your own values.
Working with Databases
To create a new database: use <database>
To show a list of all databases: show dbs
To switch to a different database: use <database>
To drop a database: use <database>; db.dropDatabase()
Working with Collections
To create a new collection: db.createCollection("<collection>")
To show a list of all collections in the current database: show collections
To drop a collection: db.<collection>.drop()
Inserting Data
To insert a single document: db.<collection>.insertOne(<document>)
To insert multiple documents: db.<collection>.insertMany([<document1>, <document2>, ...])
Querying Data
To find all documents in a collection: db.<collection>.find()
To find documents that match a specific condition: db.<collection>.find(<query>)
To limit the number of documents returned: db.<collection>.find().limit(<limit>)
To sort documents by a field: db.<collection>.find().sort({<field>: <1 or -1>})
To count the number of documents: db.<collection>.count()
Updating Data
To update a single document: db.<collection>.updateOne(<filter>, <update>)
To update multiple documents: db.<collection>.updateMany(<filter>, <update>)
To replace a document: db.<collection>.replaceOne(<filter>, <replacement>)
Deleting Data
To delete a single document: db.<collection>.deleteOne(<filter>)
To delete multiple documents: db.<collection>.deleteMany(<filter>)
To delete all documents in a collection: db.<collection>.deleteMany({})
Filters
Usage:
{ "field": { "$mod": "value" } }$eq: The$eqoperator matches documents where the value of a field equals a specified value.$ne: The$neoperator matches documents where the value of a field is not equal to a specified value.$gt: The$gtoperator matches documents where the value of a field is greater than a specified value.$gte: The$gteoperator matches documents where the value of a field is greater than or equal to a specified value.$lt: The$ltoperator matches documents where the value of a field is less than a specified value.$lte: The$lteoperator matches documents where the value of a field is less than or equal to a specified value.$in: The$inoperator matches documents where the value of a field equals any value in a specified array.$nin: The$ninoperator matches documents where the value of a field does not equal any value in a specified array.$and: The$andoperator performs a logical AND operation on an array of two or more expressions and selects the documents that satisfy all the expressions.$or: The$oroperator 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$notoperator performs a logical NOT operation on the specified expression and selects the documents that do not match the expression.$type: The$typeoperator matches documents where a specified field has a specific BSON type.$regex: The$regexoperator matches documents where a specified field matches a regular expression.$text: The$textoperator performs a text search on the specified field(s).$elemMatch: The$elemMatchoperator matches documents where a specified array field contains at least one element that matches all the specified conditions.$size: The$sizeoperator matches documents where a specified array field has a specific size.$exists: The$existsoperator matches documents that contain or do not contain a specified field, including documents where the field value isnull.
Example:
db.users.find(
{
$and: [
{ status: "active" },
{ age: { $gt: 28 } }
]
}
)Update Modifiers
$set: The$setoperator updates the value of a field in a document.$unset: The$unsetoperator removes a field from a document.$inc: The$incoperator increments the value of a field in a document.$mul: The$muloperator multiplies the value of a field in a document.$min: The$minoperator updates the value of a field in a document if the new value is lower than the existing value.$max: The$maxoperator updates the value of a field in a document if the new value is higher than the existing value.$rename: The$renameoperator renames a field in a document.$addToSet: The$addToSetoperator adds a value to an array field in a document if the value does not already exist in the array.$push: The$pushoperator appends a value to an array field in a document.$pull: The$pulloperator removes a value from an array field in a document.$currentDate: The$currentDateoperator sets the value of a field in a document to the current date and time.$each: The$each¥ operator can be used with$addToSetand$pushto append multiple values to an array field in a document.$sort: The$sortoperator can be used with$pushto sort the elements in an array field in a document.
Example:
db.users.updateOne(
{ name: "John Doe" },
{
$set: { age: 35 },
$addToSet: { interests: "Hiking" },
$unset: { status: "" }
}
)