This is my first dive into MongoDB with Docker
docker-compose.yaml
is a configuration file used with Docker Compose; used to define and manage multi-container Docker applications.
docker-compose.yaml
defines a set of services, networks, volumes, and other configurations required for the application.
-
Services: Each service corresponds to a container that is part of your application. It defines the Docker image to use, environment variables, ports to expose, volumes to mount, and more. Services can interact with each other through networking, which Docker Compose manages.
-
Networks: Networks define how the containers within the application can communicate with each other. Docker Compose automatically creates a network for your application if you don't explicitly define one. This allows containers to communicate using service names as hostnames.
-
Volumes: Volumes define where and how data should be stored and shared between containers. Volumes can be used to persist data even when containers are stopped or removed.
-
Other Configurations: The docker-compose.yaml file can also include other configurations like environment variables, restart policies, build instructions (for building custom images), and more.
Following are the list of commands to create docker containers. See docker-compose.yaml
file for mongodb docker configuration.
used to start the services defined in docker-compose.yaml
configuration file. It will create and start the containers specified in the services section of the .yml
file. If the containers do not exist, they will be built according to the images specified and then started. This command also handles linking containers, networking, and volume mounting based on the configuration you've provided. Also
docker compose -f docker-compose.yaml up
Don't need to specify default file name
docker cpmpose -f up
-f flag is used to specify the path to a Docker Compose configuration file
used to stop and remove the containers; It will stop the containers that were started with docker-compose up and remove their associated resources, including volumes, networks, and named containers. This effectively shuts down the services that were launched with docker-compose up.
starts the container in detached mode (means the containers will run in the background); creates and starts the containers according to the services defined in your docker-compose.yml file.
stops the containers that are currently running; gracefully stops the containers, allowing them to clean up resources before shutting down
starts the containers that were previously stopped using the docker compose stop command; resumes the containers from the state they were in when they were stopped.
lists and displays information about the currently running Docker containers on your system.
Using Bash shell (Mongo Shell) inside the docker container to interact with database.
to run an interactive bash shell inside the docker container; will open up a bash shell (TTY)
connects to a MongoDB server inside the MongoDB Shell using the username and password specified.
Now you can interact with the database.
Within the MongoShell, you can use following commands:
use <database>
: Creates/Switches to the specified database.show dbs
: Lists all available databases.db.stats()
: Provides statistics about the current database.db.dropDatabase()
: Deletes the current database.
db.createCollection()
: Creates a collection in the current database.show collections
: Lists all collections in the current database.db.collection.stats()
: Provides statistics about the current collection.db.collection.drop()
: Deletes the specified collection.db.collection.count()
: counts the documents in collectiondb.collection.find()
: shows all the documents
db.collection.insert()
: inserts the document (json object passed as parameter) into specifiedcollection
db.collection.insrtMany()
: inserts multiple documents
db.collection.find({<field value(s)>})
: finds the document that matches the provided field values
btDB> db.student.find({firstName:"Sophia"},{firstName: 1, favoriteSubjects: 1})
[
{
_id: ObjectId("64e3b2ad38d56c69f4f69607"),
firstName: 'Sophia',
favoriteSubjects: [ 'history', 'anthropology', 'archaeology' ]
}
]
db.collection.find({field value(s)},{projection})
: finds the document that matches the criteria, and displays in specified projection To exclude any field, use -1.
Updates are done by providing the unique keywords for type of updates, along with ObjectId, and update field/value pair.
db.collection.update({_id: ObjectId("<id>")}, {$<keyword>: {<update parameters & values>}})
Following are the list of keywords and their corresponding update operations:
$set
finds the match and updates$unset
removes/deletes the specified field$inc: {<parameter>: N}
increments the parameter (type double) byN
To update the elements of an array field use push
& pull
:
$push: {arrFieldName: fieldVal}
inserts new value in thearrFieldName
$pull: {arrFieldName: fieldVal}
removesfieldVal
from thearrfieldName
db.collection.deleteOne(_id: ObjectId("<id>"))
deletes the object of given_id
db.collection.deleteOne({})
only deletes one doc objectdb.collection.deleteMany({field: value})
deletes all the object matching given field/value pair
MongoDB Collection documents uses BSON data types. BSON (Binary JSON) is a binary-encoded serialization format used to store and exchange data in a lightweight and efficient manner.
-
Double: Represents a double-precision floating-point number.
-
String: Represents a sequence of UTF-8 encoded characters.
-
Object: Represents an embedded document within a BSON document. This is similar to a JSON object.
-
Array: Represents an ordered list of values, similar to a JSON array.
-
Binary Data: Represents binary data, such as images or files.
-
Boolean: Represents a boolean value (true or false).
-
DateTime: Represents a 64-bit integer, typically representing the number of milliseconds since the Unix epoch.
-
Null: Represents a null value.
-
Regular Expression: Represents a regular expression pattern and its options.
-
JavaScript Code: Represents JavaScript code or a script.
-
Symbol: Represents a symbol, which is a unique identifier.
-
JavaScript Code with Scope: Represents JavaScript code with an associated scope.
-
32-bit Integer: Represents a 32-bit integer.
-
Timestamp: Represents a special BSON data type used for timestamps.
-
64-bit Integer: Represents a 64-bit integer.
-
Decimal128: Represents a 128-bit decimal floating-point value.
-
Min Key: Represents the smallest possible key value.
-
Max Key: Represents the largest possible key value.