How to use Azure Cosmos Emulator as a local MongoDb database 🌍 (2024)

I recently was trying to prepare a demo which involved me having a local MongoDb database. I reviewed a few options and was about to choose one when I remembered I have Azure Cosmos DB Emulator installed and Cosmos DB supports MongoDb APIs.

Install if you haven't already

You will need below requirements to be able to install the emulator:

  • Windows Server 2012 R2, Windows Server 2016, 2019 or Windows 8 and 10. Docker on Windows, Linux and macOS is also supported.
  • 64-bit OS.
  • Minimum 2GB RAM.
  • At least 10GB available hard disk space.

If you've ticked all of above already, then head over here and install the latest version. If you stumbled upon any issues use the troubleshooting guide to find out what's happening.

πŸ’‘ Make sure you check for updates regularly since each new version might contain new features and bug fixes.

Starting the emulator

Normally you would use the Windows start menu to find your programs and start the application, however, for the emulator to support MongoDB APIs, you will need to pass the EnableMongoDbEndpoint argument.

How to use Azure Cosmos Emulator as a local MongoDb database 🌍 (1)

You have a few options here:

  1. Create a shortcut and pass the required arguments
  2. Use command line to start it

I am going to use Windows Terminal. Run the below command to fire up the emulator with MongoDB support:

& 'C:\Program Files\Azure Cosmos DB Emulator\CosmosDB.Emulator.exe' /EnableMongoDbEndpoint

You will be prompted to approve the emulator making changes to your system and once approved, you will see the emulator starting and the interface will open in your default browser.

Getting the connection string

The very first thing you'd see is the quick start which should look something like this:

How to use Azure Cosmos Emulator as a local MongoDb database 🌍 (2)

What you'll need is the Mongo Connection String. Copy that you'll be ready to kick start. I am going to use the Node sample from the quick start to continue the rest of this guide, but you can use this straight away in your current code.

Creating a Node.js Web API project

Open up a command prompt in a new folder and initiate a project:

npm init -y

The next thing to do is to install the dependencies:

# linux or macOStouch app.js# Windowsecho "" > app.js# install dependenciesnpm install express mongodb body-parser

The app

We would have a simple application, since this tutorial is not a Node.js or Express.js focused, I will just brush on some steps here. Feel free to go through and understand each of these at your own pace.

const Express = require("express");const BodyParser = require("body-parser");const MongoClient = require("mongodb").MongoClient;const ObjectId = require("mongodb").ObjectID;var app = Express();app.use(BodyParser.json());app.use(BodyParser.urlencoded({ extended: true }));app.listen(5000, () => {});

What we have here is a simple web server which responds to http://localhost:5000. All we're doing is initializing the MongoClient and a web server. Next thing we need to do is to establish a connection with MongoDB interface of our emulator:

const Express = require("express");const BodyParser = require("body-parser");const MongoClient = require("mongodb").MongoClient;const ObjectId = require("mongodb").ObjectID;const CONNECTION_URL = "YOUR CONNECTION STRING";const DATABASE_NAME = "cosmos_emulator_mongo";var app = Express();app.use(BodyParser.json());app.use(BodyParser.urlencoded({ extended: true }));var database, collection;app.listen(5000, () => { MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => { if(error) { throw error; } database = client.db(DATABASE_NAME); collection = database.collection("celebrities"); console.log("Connected to `" + DATABASE_NAME + "`!"); });});

The next thing we do to make it simpler to run the app is to add a start script to our package.json:

"scripts": { "start": "node app.js"}

If you run npm start now, you should see the message connected to Connected to 'cosmos_emulator_mongo' printed on your console which means you've successfully connected to the MongoDb APIs.

Seed the database

First let's create a JSON file which contains some celebrities 😎:

[{ "age": 18, "birth_place": "Harij, Gujarat, India", "birth_sign": "Pisces", "birth_year": "1996", "birthday": "September 28", "name": "Daxeel Soni", "occupation": "Developer of this API. More: www.daxeelsoni.in", "photo_url": "http://daxeelsoni.in/images/me.jpg"}, { "age": 28, "birth_place": "Houston", "birth_sign": "Libra", "birth_year": "1987", "birthday": "September 28", "name": "Hilary Duff", "occupation": "TV Actress", "photo_url": "http://www.famousbirthdays.com/thumbnails/duff-hilary-medium.jpg"}, { "age": 38, "birth_place": "Columbia", "birth_sign": "Libra", "birth_year": "1977", "birthday": "September 28", "name": "Young Jeezy", "occupation": "Rapper", "photo_url": "http://www.famousbirthdays.com/thumbnails/jeezy-young-medium.jpg"}, { "age": 48, "birth_place": "Roanoke", "birth_sign": "Libra", "birth_year": "1967", "birthday": "September 28", "name": "Challen Cates", "occupation": "TV Actress", "photo_url": "http://www.famousbirthdays.com/thumbnails/cates-challen-medium.jpg"}]

Now that we have our JSON file, let's seed the database. Replace the initial connect code with this:

app.listen(5000, () => { MongoClient.connect( CONNECTION_URL, { useNewUrlParser: true, sslValidate: false, }, (error, client) => { if (error) { throw error; } database = client.db(DATABASE_NAME); collection = database.collection("celebrities"); collection.estimatedDocumentCount({}, (erorr, numOfRecords) => { if (numOfRecords <= 0) { fs.readFile("./info.json", "utf8", (err, data) => { if (err) { console.log(`Error reading file from disk: ${err}`); } else { // parse JSON string to JSON object const celebrities = JSON.parse(data); collection.insertMany(celebrities, (error, result) => { if (error) { console.log(`Error in saving seed data: ${error}`); } console.log(`Seed data inserted into the database!`); }); } }); } else { console.log(`Connected to database`); } }); } );});

All we're doing here is to get the number of records in the collection and if there is nothing, read the JSON file and write it to the collection.

Don't forget to add const fs = require('fs') at the top with other require statements.

Now if you run npm start would should see the application start and then the seed data inserted message.

Creating the endpoint

And now all we need is to add our get endpoint to be able to fetch our celebrities. Don't forget to add it before you create your listener:

app.get("/api/celebrities", (request, response) => { collection.find({}).toArray((error, result) => { if(error) { return response.status(500).send(error); } response.send(result); });});

Testing the application

You're now ready to get your celebrities via the API. Simply open a browser and head over to http://localhost:5000/api/celebrities or just use the below command:

curl -X GET http://localhost:5000/api/celebrities

Summary

In this guide we saw how to use Azure Cosmos DB Emulator with MongoDb API to have a local MongoDb available for our local development and proof of concepts. Hope this has helped you save some time and also the trouble of installing yet another software on your system (of course if you already are using the emulator 😊).

Enjoy hacking and let us know what awesome projects you're doing using the emulator.

PS: You can find the finished code in my GitHub repository here.

How to use Azure Cosmos Emulator as a local MongoDb database 🌍 (2024)

References

Top Articles
Leeks - Nutrition Facts, Health Benefits, Recipes and Substitutes
How to Select and Use Leeks (with Pictures)
Creepshotorg
Promotional Code For Spades Royale
What to Serve with Lasagna (80+ side dishes and wine pairings)
Coindraw App
Google Jobs Denver
How to know if a financial advisor is good?
Horoscopes and Astrology by Yasmin Boland - Yahoo Lifestyle
South Carolina defeats Caitlin Clark and Iowa to win national championship and complete perfect season
Mylaheychart Login
Richard Sambade Obituary
Tv Schedule Today No Cable
Johnston v. State, 2023 MT 20
About Us | TQL Careers
Lax Arrivals Volaris
The Banshees Of Inisherin Showtimes Near Regal Thornton Place
Hilo Hi Craigslist
Gdlauncher Downloading Game Files Loop
Msu 247 Football
Mikayla Campinos Laek: The Rising Star Of Social Media
Music Go Round Music Store
Vegas7Games.com
Craigslist Pearl Ms
Minnick Funeral Home West Point Nebraska
How Long After Dayquil Can I Take Benadryl
How to Watch Every NFL Football Game on a Streaming Service
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Jackie Knust Wendel
Labcorp.leavepro.com
Violent Night Showtimes Near Johnstown Movieplex
Vht Shortener
Encore Atlanta Cheer Competition
Best Town Hall 11
Meowiarty Puzzle
N.J. Hogenkamp Sons Funeral Home | Saint Henry, Ohio
Grandstand 13 Fenway
Ixlggusd
Roch Hodech Nissan 2023
Jay Gould co*ck
Barrage Enhancement Lost Ark
Junee Warehouse | Imamother
Bimmerpost version for Porsche forum?
Planet Fitness Lebanon Nh
Spectrum Outage in Genoa City, Wisconsin
Dr Adj Redist Cadv Prin Amex Charge
Nu Carnival Scenes
Christie Ileto Wedding
Mikayla Campinos Alive Or Dead
Fetllife Com
Room For Easels And Canvas Crossword Clue
Pauline Frommer's Paris 2007 (Pauline Frommer Guides) - SILO.PUB
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6158

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.