Working with MongoDB
MongoDB is a popular document database that is commonly used in JavaScript applications. The project in the "start" folder has been modified to write values to a MongoDB database. That database doesn't exist. You'll need to create it, run the application a few times, and then query it to ensure that there are records being saved.
Open the Cosmos DB extension
Connect to "mongodb://mongo"
Create a new database called "lamp"
Create a new collection called "colors"
Run the application with
npm startChange the colors a few times
Open a new Mongo DB scrapbook
Query the "colors" collection to see the records
Open the Cosmos DB extension by clicking on the Azure icon in the Activity Bar
Expand the "Attached Database Accounts" node

Select "Attach Database Account"

Select MongoDB from the prompt

In the prompt, enter "mongodb://mongo"

Expand the newly attached local Mongo DB database

Right-click the newly attached MongoDB database and select "Create Database"

Name the database "lamp" in the prompt

Enter "colors" for the collection name in the prompt

Open the Integrated Terminal (Cmd/Ctrl + `)
Run the application with
npm startBrowse to "localhost:3000"

Change the color a few times and click the "Go" button
Make sure you repeat this process a few times
Return to VS Code and look in the Cosmos DB extension
Expand the "lamp" database and "colors" collection
Each document listed inside is one of the colors that was saved from the app

Click on one of the documents to open it in the editor

Change the "color" value and save the document with (Cmd/Ctrl + S)
VS Code will prompt you that it's going to save this record back to the database

Select "Upload"
The document is saved back to Mongo DB

Open the Command Palette (Cmd/Ctrl + Shift + P)
Select "Cosmos DB: New Mongo Scrapbook"

Begin writing a new query by typing "db" and then hitting the period. Notice that VS Code has intellisense for any collections or methods available.

Select "colors" from the list
Type a find query for the colors collection
db.colors.find()Execute the query with Cmd/Ctrl + Shift + '
View the query results in split pane mode

Change the color in the first record
Save the document with Cmd/Ctrl + S
Select "Upload" from the prompt
The changes are persisted to Mongo DB

Last updated
Was this helpful?