In this article, we are going to understand MongoDB along with its Docker Composer and its Visual Studio Code extension playground.
(Step 2):
(Step 4):
Let's fetch documents by filtering document properties value
MongoDB:
MongoDB is a source-available cross-platform document-oriented database. It is also called a NoSQL database or Non-Relational database.
In MongoDB 'Collection' is equivalent to the Table in SQL database.
In MongoDB data is stored as 'Document' that is equivalent to a table record in an SQL database. The 'Document' contains JSON data which will be stored in BSON(Binary JSON) format.
MongoDB Docker Composer:
Let's implement docker-compose to create a MongoDB docker container.
(Step 1):
Download and install the 'Docker Desktop' application. The application should be in running mode.
Add a new folder for our MongoDB docker composer. Then add the file like 'docker-compose.yml'.
docker-compose.yml:
version: '3.8' services: mongodb: image: mongo:5.0 volumes: - ./mongo/data:/data/db ports: - 8007:27017 container_name: mymongo
- (Line: 1) The 'version' defines the version number of the docker composer.
- (Line: 5) Specified the 'mongo' image.
- (Line: 6-7) Defining the 'volumes' where we can specify the physical locations. Here first part './mongo/data' is the folder from your current location of the 'docker.compose.yml' and the second part '/data/db' is the default folder location of the 'mongo' docker image.
- (Line: 8-9) The default number of the MongoDB is '27017', here I'm exposing to a custom port number that is '8007'.
- (Line: 10) The 'container_name' property to define our docker container name.
Now open the command prompt under the path of the 'docker-compose.yml' and run the following command.
docker-compose up -d
On successful command execution, we can observe the container gets created and runs.
Visual Studio Code Extension For MongoDB:
We can use visual studio code extension for MongoDB as a UI interface for the Mongo queries.
(Step 1):
Install VS Code extension 'MongoDB for VS Code'.
(Step 2):
Now Open the 'MongoDB for VS Code'. Here we have 2 options 'CONNECTIONS' and 'PLAYGROUNDS'. Click on the '+' icon on the 'CONNECTIONS' tab and click on the 'Open form' button.
(Step 3):
Enter the port number we configured in our docker-compose file and then click on connect button.
Open the playground window and then query the MongoDB queries.
Query To Create Database And Collection:
Let's understand MongoDB queries to create a database and collection.
use('myworlddb') db.createCollection('person')
- (Line: 1) The 'Use' command to create the database.
- (Line: 3) The 'db.createCollection()' creates the new collection to our database.
Query To Insert One Document:
Let's implement a MongoDB query to insert a single document.
use('myworlddb') db.person.insertOne({ name:"naveen", age: 28 })
- Here inserting a new document into the 'person' collection using the 'insertOne({})' method.
Query To Insert Multiple Documents:
Let's implement a MongoDB query to insert multiple documents into the collection.
use('myworlddb') db.person.insertMany([ { name:"hemanth", age: 32 }, { name:"kumar", age: 40 } ])
- Using the 'insertMany()' method we can create multiple documents into the collection.
Query To Fetch The Documents:
Let's implement a query to fetch the documents.
use('myworlddb') //{} empty fetches all documents db.person.find({})
- The 'find()' method with empty '{}' fetches all the documents from the collection.
Let's fetch documents by filtering document properties value
use('myworlddb') //{} empty fetches all documents db.person.find({name:"naveen"})
- Here try to fetch the document by matching the value of the property 'name'.
Let's fetch documents by using the comparison operators like 'lt'(less than), 'lte'(less than equals), 'gt'(greater than), etc.
use('myworlddb') //{} empty fetches all documents db.person.find({age:{$gt:30}})
- Here we used 'gt'(greater than) operator to compare age documents. While querying this operator should be prefixed with '$'.
Query To Update The Single Document:
Let's implement a query to update the single document.
use('myworlddb') //{} empty fetches all documents db.person.updateOne({name:"hemanth"},{ $set:{age: 28} })
- Here 'updateOne()' method 1st parameter of our filter object to find the document to be modified and 2nd parameter contains property '$set' which object that contains properties to be updated.
Query To Update The Multiple Documents:
Let's implement a query to update multiple documents.
use('myworlddb') //{} empty fetches all documents db.person.updateMany({age:{$lt:30}},{ $set:{age: 1} })
Query To Delete One Document:
Let's implement a query to delete a single document.
use('myworlddb') //{} empty fetches all documents db.person.deleteOne({name:"hemanth"})
Query To Delete Multiple Documents:
Let's implement a query to delete multiple documents.
use('myworlddb') //{} empty fetches all documents db.person.deleteMany({name:"hemanth"})
- Here 'deleteMany()' method deletes all matched documents from the collection.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on MongoDB with Docker and Visual Studio Code. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Comments
Post a Comment