How to work with the MongoDB in VS-Code?

Introduction

I'm going to explain a new topic about How to Work with MongoDB in VS-Code. In my last article, I explained How to Create a MongoDB Atlas Account. Did not read the article, so I can share the link here.

I'm not explaining how to install VS-Code; I hope you have experience working with VS-Code.  

Let's start with the Topic.

You do not have any idea about MongoDB Atlas? Please go through my article above; I'm going to continue with my article.

How to get the Connection String?

Step 1. Click on Connect button in the cluster in the dashboard.

Database Deployment

Picture 01. You can view the current cluster, and  You can navigate to the cluster.

Step 2. Click on the MongoDB for VS Code Option.

Connect to MongoDB

Picture 02

Step 3. Get the Connect to MongoDBCluster01 window.

MongoDB Cluster

Picture 03. Get your connection string "Connect to your MongoDB deployment" I highlight the green color, and Copy it.

Now we are going to install MongoDB for VS Code let's start. 

Install MongoDB VS Code Extension

MongoDB support for VS Code is provided by the MongoDB for VS Code extension, which lets you work with MongoDB and your data directly within your coding environment. 

This extension allows us to create playgrounds to perform query operations on our Document(data) and collections and gives us the results quickly at the click of a button.

Step 01. Install the extension.

Open the VS Code, go to click the Extensions or the Extensions view by pressing Ctrl+Shift+X, and search for 'MongoDB for VS Code' to filter the results.

MongoDB in VS Code

Picture 04. Get the search result and see the picture and click the install button.

Step 02. After the extension installation. 

  1. Go to the left sidebar menu and click the leaf icon.
  2. Click the add connection button.
  3. Click on the MongoDB Command Palette option. 
    MongoDB Add Connection

Picture 05. Follow the order mentioned in the picture by the number.

Step 03.  You can paste the connection string from the MongoDB Atlas, as I explained in Picture 03.

MongoDB Add Connection

Picture 06. You can copy and paste the connection from the MongoDB Atlas connection, 

Step 04. You can view the database from MongoDB Atlas.

MongoDB Add Connection

Picture 07. Now You are seeing the databases were at the VS-Code from the MongoDB Atlas.

You can expand databases to view their collections. with you can see individual MongoDB Documents to view their JSON.

MongoDB in VS Code

Picture 08. You can see the individual MongoDB Documents to view their JSON.

Now we can work with  MongoDB Atlas. 

  1. Create a Database.
  2. Create a Collection.
  3. Insert, Update, Delete, and View Operations. 

We can be Creating a Database with Collections using the MongoDB snippets in vs-code. 

We can use the MongoDB Playground as well. Let's start... 

Let's Play with MongoDB Atlas

Create a Database with a Collection

Step 5. Right, Click on the DB in your vs-code then after, you will get the pop window; you can select Add Database or Click the plus icon.

Update result MongoDB

Picture 9. This picture explains the pop window; there are Rename, Disconnect, Remove the connection, and More..,

Step 6.  When you touch step 5, you will see the playground-1.mongodb.js file; then, You can enter the Database and Collection, which I will explain here.

const database = 'NEW_DATABASE_NAME'; 
const collection = 'NEW_COLLECTION_NAME';
// Create a new database.
use(database);
// Create a new collection.
db.createCollection(collection);

Now you can edit the following line.

// Create a new database.
use(database); => this line create a database
// Create a new collection.
db.createCollection(collection); => this line create a collection

MongoDB  Connection

Picture 10. I explain the number 4 with a sample code here. The method has the following prototype.

// The prototype form to create a collection:
 db.createCollection( <Student_info>,  <- collection
  {
    firstName: <string>,  <-- Field :  value
    lastName: <string>,   <-- Field :  value
    Address: <string>,    <-- Field :  value
    contect_num: <number>, <-- Field :  value
 }
)

Step 7.  Enter the Database and Collection name. You can press the play icon.

MongoDB Connection

Picture 11. Press the play icon Creating the Database and Collection. 

When creating the database, you will get the Playground Result.

MongoDB Playground

Picture 12. Playground Result.

MongoDB Playground

Picture 13. Database and Collection are created. 

MongoDB in Cluster

Picture 14. Database and Collection are created. 

Insert the data

use('csharp_corner_learning')

db.student_info.insertOne({
    name: 'mayooran',
    address: 'Kalmunai',
    age:25
})
db.student_info.find();

You can see the code here, and it is easily understood, 

  • The first line says: it's called the database.
  • The second line says: It's called Collection (student_info) and inserts the field and value.
  • The third line says: Get the document view.

Step 8.  You can press the Play icon after you can get the Playground result here.

MongoDB Add Connection

Picture 15.   Get the Result from this line db.student_info.find(); You can see the Playground result here.

MongoDB Playground

Picture 16.   See the Document from MongoDB Atlas csharp_corner_learning.student_info. 

Update the data

use("csharp_corner_learning");

db.student_info.updateOne(
  {
    _id: ObjectId("64a685757acccfac3d045acf"),
  },
  {
    $set: {
      name: "Mayooran Navamany",
      address: "Kalmunai 02",
      age: 25,
    },
  }
);

db.student_info.find();

The ObjectId is auto-generated; when inserting the document, the id will be auto-generated. When the update query executes, Find the Id and make an update. Set the values using the $set keyword and update the data.

Update data in MongoDB

Picture 17. You can see the Updated Result here.

Update result MongoDB

Picture 18.   See the Updated Document from MongoDB Atlas csharp_corner_learning.student_info. 

Delete the Data

use("csharp_corner_learning");

db.student_info.deleteOne(
  {
    _id: ObjectId("64a685757acccfac3d045acf"),
  }
);

db.student_info.find();

When the delete query executes, Find the Id and delete the data.

Update result MongoDB

Picture 19.  Find the Id and delete the data. See the Result empty.

Summary

In this article, I explained How to work with MongoDB in VS-Code, Create a Database, Collection, and Document. How to Insert, View, Update, and Delete using query operations. I hope this article helps those Who have an interest in self-learning. Keeping in touch with me, I'll publish Advance level articles in node-js and connect to MongoDB for beginners.


Similar Articles
Orfium Sri Lanka
Globally based Software Developing & Data Processing Company