{"componentChunkName":"component---src-templates-post-js","path":"/blog/2020/11/30/az-cosmos-emulator/","result":{"data":{"post":{"html":"<p>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 <a href=\"https://yas.fyi/azcosemu\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Azure Cosmos DB Emulator</a> installed and Cosmos DB supports MongoDb APIs.</p>\n<!--more-->\n<h2 id=\"install-if-you-havent-already\" style=\"position:relative;\"><a href=\"#install-if-you-havent-already\" aria-label=\"install if you havent already permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Install if you haven’t already</h2>\n<p>You will need below requirements to be able to install the emulator:</p>\n<ul>\n<li>Windows Server 2012 R2, Windows Server 2016, 2019 or Windows 8 and 10. Docker on Windows, Linux and macOS is also supported.</li>\n<li>64-bit OS.</li>\n<li>Minimum 2GB RAM.</li>\n<li>At least 10GB available hard disk space.</li>\n</ul>\n<p>If you’ve ticked all of above already, then head <a href=\"https://aka.ms/cosmosdb-emulator\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">over here and install the latest version</a>. If you stumbled upon any issues use <a href=\"https://docs.microsoft.com/en-us/azure/cosmos-db/troubleshoot-local-emulator\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">the troubleshooting guide</a> to find out what’s happening.</p>\n<blockquote>\n<p>💡 Make sure you check for updates regularly since each new version might contain new features and bug fixes.</p>\n</blockquote>\n<h2 id=\"starting-the-emulator\" style=\"position:relative;\"><a href=\"#starting-the-emulator\" aria-label=\"starting the emulator permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Starting the emulator</h2>\n<p>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 <code class=\"language-text\">EnableMongoDbEndpoint</code> argument.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1080px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/4de7592b693bf405cbfca0e60aeb55dc/d8309/startingthecosmosdbemulator.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 81.85185185185185%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAQABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAwAF/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhADEAAAAdJERBnl/8QAGRAAAgMBAAAAAAAAAAAAAAAAABMBAgMQ/9oACAEBAAEFAk0E5iciO//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABoQAAICAwAAAAAAAAAAAAAAAACRAjIBIDP/2gAIAQEABj8CrhFIo5xWn//EABsQAAEEAwAAAAAAAAAAAAAAAAABEBEhceHx/9oACAEBAAE/IU08rqfE5diN/9oADAMBAAIAAwAAABBcz//EABYRAQEBAAAAAAAAAAAAAAAAAAEQEf/aAAgBAwEBPxAMn//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EAB4QAAIBAwUAAAAAAAAAAAAAAAERACEx0UFRYeHw/9oACAEBAAE/EMnwxoCzM9E3PJxA1RcACrqoxP/Z'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Starting Azure Cosmos DB Emulator\"\n        title=\"\"\n        src=\"/static/4de7592b693bf405cbfca0e60aeb55dc/47311/startingthecosmosdbemulator.jpg\"\n        srcset=\"/static/4de7592b693bf405cbfca0e60aeb55dc/6f81f/startingthecosmosdbemulator.jpg 270w,\n/static/4de7592b693bf405cbfca0e60aeb55dc/09d21/startingthecosmosdbemulator.jpg 540w,\n/static/4de7592b693bf405cbfca0e60aeb55dc/47311/startingthecosmosdbemulator.jpg 1080w,\n/static/4de7592b693bf405cbfca0e60aeb55dc/d8309/startingthecosmosdbemulator.jpg 1165w\"\n        sizes=\"(max-width: 1080px) 100vw, 1080px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>You have a few options here:</p>\n<ol>\n<li>Create a shortcut and pass the required arguments</li>\n<li>Use command line to start it</li>\n</ol>\n<p>I am going to use <a href=\"https://github.com/microsoft/terminal\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Windows Terminal</a>. Run the below command to fire up the emulator with MongoDB support:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"19830305658650026000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`& 'C:\\Program Files\\Azure Cosmos DB Emulator\\CosmosDB.Emulator.exe' /EnableMongoDbEndpoint`, `19830305658650026000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-bash line-numbers\"><code class=\"language-bash\"><span class=\"token operator\">&amp;</span> <span class=\"token string\">'C:\\Program Files\\Azure Cosmos DB Emulator\\CosmosDB.Emulator.exe'</span> /EnableMongoDbEndpoint</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>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.</p>\n<h2 id=\"getting-the-connection-string\" style=\"position:relative;\"><a href=\"#getting-the-connection-string\" aria-label=\"getting the connection string permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting the connection string</h2>\n<p>The very first thing you’d see is the quick start which should look something like this:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1080px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/1ded639cfb17580528aea8c832a21acb/57b36/emulatorquickstart.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 50%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAKABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAwABBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAei+uDNH/8QAGhAAAgMBAQAAAAAAAAAAAAAAAgMAARMQM//aAAgBAQABBQJYVa8xmYRPjz//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAcEAABAwUAAAAAAAAAAAAAAAACADKRECIxUXH/2gAIAQEABj8CG0caTAhMGEHK/wD/xAAdEAABAgcAAAAAAAAAAAAAAAAAARARITFBobHx/9oACAEBAAE/IVziy1KXgcgYHT//2gAMAwEAAgADAAAAEOAP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAHRABAAEDBQAAAAAAAAAAAAAAAQARYfAQQVHB0f/aAAgBAQABPxA+uVUK1oWnUh8hv41pn+Gv/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Azure Cosmos DB Emulator connection string page\"\n        title=\"\"\n        src=\"/static/1ded639cfb17580528aea8c832a21acb/47311/emulatorquickstart.jpg\"\n        srcset=\"/static/1ded639cfb17580528aea8c832a21acb/6f81f/emulatorquickstart.jpg 270w,\n/static/1ded639cfb17580528aea8c832a21acb/09d21/emulatorquickstart.jpg 540w,\n/static/1ded639cfb17580528aea8c832a21acb/47311/emulatorquickstart.jpg 1080w,\n/static/1ded639cfb17580528aea8c832a21acb/0047d/emulatorquickstart.jpg 1620w,\n/static/1ded639cfb17580528aea8c832a21acb/274e1/emulatorquickstart.jpg 2160w,\n/static/1ded639cfb17580528aea8c832a21acb/57b36/emulatorquickstart.jpg 2425w\"\n        sizes=\"(max-width: 1080px) 100vw, 1080px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>What you’ll need is the <em>Mongo Connection String</em>. 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.</p>\n<h2 id=\"creating-a-nodejs-web-api-project\" style=\"position:relative;\"><a href=\"#creating-a-nodejs-web-api-project\" aria-label=\"creating a nodejs web api project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Creating a Node.js Web API project</h2>\n<p>Open up a command prompt in a new folder and initiate a project:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"89534858195444350000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`npm init -y`, `89534858195444350000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-bash line-numbers\"><code class=\"language-bash\"><span class=\"token function\">npm</span> init <span class=\"token parameter variable\">-y</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>The next thing to do is to install the dependencies:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"50119106085919056000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`# linux or macOS\r\ntouch app.js\r\n\r\n# Windows\r\necho &quot;&quot; > app.js\r\n\r\n# install dependencies\r\nnpm install express mongodb body-parser`, `50119106085919056000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-bash line-numbers\"><code class=\"language-bash\"><span class=\"token comment\"># linux or macOS</span>\r\n<span class=\"token function\">touch</span> app.js\r\n\r\n<span class=\"token comment\"># Windows</span>\r\n<span class=\"token builtin class-name\">echo</span> <span class=\"token string\">\"\"</span> <span class=\"token operator\">></span> app.js\r\n\r\n<span class=\"token comment\"># install dependencies</span>\r\n<span class=\"token function\">npm</span> <span class=\"token function\">install</span> express mongodb body-parser</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"the-app\" style=\"position:relative;\"><a href=\"#the-app\" aria-label=\"the app permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The app</h3>\n<p>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.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"34222211822716166000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const Express = require(&quot;express&quot;);\r\nconst BodyParser = require(&quot;body-parser&quot;);\r\nconst MongoClient = require(&quot;mongodb&quot;).MongoClient;\r\nconst ObjectId = require(&quot;mongodb&quot;).ObjectID;\r\nvar app = Express();\r\napp.use(BodyParser.json());\r\napp.use(BodyParser.urlencoded({ extended: true }));\r\napp.listen(5000, () => {});`, `34222211822716166000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> Express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"express\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> BodyParser <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"body-parser\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> MongoClient <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"mongodb\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>MongoClient<span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> ObjectId <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"mongodb\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>ObjectID<span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">var</span> app <span class=\"token operator\">=</span> <span class=\"token function\">Express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>BodyParser<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>BodyParser<span class=\"token punctuation\">.</span><span class=\"token function\">urlencoded</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">extended</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token number\">5000</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>What we have here is a simple web server which responds to <code class=\"language-text\">http://localhost:5000</code>. All we’re doing is initializing the <code class=\"language-text\">MongoClient</code> and a web server. Next thing we need to do is to establish a connection with MongoDB interface of our emulator:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"11042735519240399000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const Express = require(&quot;express&quot;);\r\nconst BodyParser = require(&quot;body-parser&quot;);\r\nconst MongoClient = require(&quot;mongodb&quot;).MongoClient;\r\nconst ObjectId = require(&quot;mongodb&quot;).ObjectID;\r\nconst CONNECTION_URL = &quot;YOUR CONNECTION STRING&quot;;\r\nconst DATABASE_NAME = &quot;cosmos_emulator_mongo&quot;;\r\n\r\n\r\nvar app = Express();\r\napp.use(BodyParser.json());\r\napp.use(BodyParser.urlencoded({ extended: true }));\r\nvar database, collection;\r\n\r\napp.listen(5000, () => {\r\n    MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => {\r\n        if(error) {\r\n            throw error;\r\n        }\r\n        database = client.db(DATABASE_NAME);\r\n        collection = database.collection(&quot;celebrities&quot;);\r\n        console.log(&quot;Connected to \\`&quot; + DATABASE_NAME + &quot;\\`!&quot;);\r\n    });\r\n});`, `11042735519240399000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> Express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"express\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> BodyParser <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"body-parser\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> MongoClient <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"mongodb\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>MongoClient<span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> ObjectId <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"mongodb\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>ObjectID<span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> <span class=\"token constant\">CONNECTION_URL</span> <span class=\"token operator\">=</span> <span class=\"token string\">\"YOUR CONNECTION STRING\"</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> <span class=\"token constant\">DATABASE_NAME</span> <span class=\"token operator\">=</span> <span class=\"token string\">\"cosmos_emulator_mongo\"</span><span class=\"token punctuation\">;</span>\r\n\r\n\r\n<span class=\"token keyword\">var</span> app <span class=\"token operator\">=</span> <span class=\"token function\">Express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>BodyParser<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>BodyParser<span class=\"token punctuation\">.</span><span class=\"token function\">urlencoded</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">extended</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">var</span> database<span class=\"token punctuation\">,</span> collection<span class=\"token punctuation\">;</span>\r\n\r\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token number\">5000</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    MongoClient<span class=\"token punctuation\">.</span><span class=\"token function\">connect</span><span class=\"token punctuation\">(</span><span class=\"token constant\">CONNECTION_URL</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">useNewUrlParser</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> client</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n            <span class=\"token keyword\">throw</span> error<span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span>\r\n        database <span class=\"token operator\">=</span> client<span class=\"token punctuation\">.</span><span class=\"token function\">db</span><span class=\"token punctuation\">(</span><span class=\"token constant\">DATABASE_NAME</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n        collection <span class=\"token operator\">=</span> database<span class=\"token punctuation\">.</span><span class=\"token function\">collection</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"celebrities\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Connected to `\"</span> <span class=\"token operator\">+</span> <span class=\"token constant\">DATABASE_NAME</span> <span class=\"token operator\">+</span> <span class=\"token string\">\"`!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>The next thing we do to make it simpler to run the app is to add a start script to our <code class=\"language-text\">package.json</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"30007803768766950000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`&quot;scripts&quot;: {\r\n    &quot;start&quot;: &quot;node app.js&quot;\r\n}`, `30007803768766950000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-json line-numbers\"><code class=\"language-json\"><span class=\"token property\">\"scripts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">\"start\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"node app.js\"</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>If you run <code class=\"language-text\">npm start</code> now, you should see the message connected to <code class=\"language-text\">Connected to 'cosmos_emulator_mongo'</code> printed on your console which means you’ve successfully connected to the MongoDb APIs.</p>\n<h3 id=\"seed-the-database\" style=\"position:relative;\"><a href=\"#seed-the-database\" aria-label=\"seed the database permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Seed the database</h3>\n<p>First let’s create a JSON file which contains some celebrities 😎:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"34919846520671040000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`[{\r\n  &quot;age&quot;: 18,\r\n  &quot;birth_place&quot;: &quot;Harij, Gujarat, India&quot;,\r\n  &quot;birth_sign&quot;: &quot;Pisces&quot;,\r\n  &quot;birth_year&quot;: &quot;1996&quot;,\r\n  &quot;birthday&quot;: &quot;September 28&quot;,\r\n  &quot;name&quot;: &quot;Daxeel Soni&quot;,\r\n  &quot;occupation&quot;: &quot;Developer of this API. More: www.daxeelsoni.in&quot;,\r\n  &quot;photo_url&quot;: &quot;http://daxeelsoni.in/images/me.jpg&quot;\r\n}, {\r\n  &quot;age&quot;: 28,\r\n  &quot;birth_place&quot;: &quot;Houston&quot;,\r\n  &quot;birth_sign&quot;: &quot;Libra&quot;,\r\n  &quot;birth_year&quot;: &quot;1987&quot;,\r\n  &quot;birthday&quot;: &quot;September 28&quot;,\r\n  &quot;name&quot;: &quot;Hilary Duff&quot;,\r\n  &quot;occupation&quot;: &quot;TV Actress&quot;,\r\n  &quot;photo_url&quot;: &quot;http://www.famousbirthdays.com/thumbnails/duff-hilary-medium.jpg&quot;\r\n}, {\r\n  &quot;age&quot;: 38,\r\n  &quot;birth_place&quot;: &quot;Columbia&quot;,\r\n  &quot;birth_sign&quot;: &quot;Libra&quot;,\r\n  &quot;birth_year&quot;: &quot;1977&quot;,\r\n  &quot;birthday&quot;: &quot;September 28&quot;,\r\n  &quot;name&quot;: &quot;Young Jeezy&quot;,\r\n  &quot;occupation&quot;: &quot;Rapper&quot;,\r\n  &quot;photo_url&quot;: &quot;http://www.famousbirthdays.com/thumbnails/jeezy-young-medium.jpg&quot;\r\n}, {\r\n  &quot;age&quot;: 48,\r\n  &quot;birth_place&quot;: &quot;Roanoke&quot;,\r\n  &quot;birth_sign&quot;: &quot;Libra&quot;,\r\n  &quot;birth_year&quot;: &quot;1967&quot;,\r\n  &quot;birthday&quot;: &quot;September 28&quot;,\r\n  &quot;name&quot;: &quot;Challen Cates&quot;,\r\n  &quot;occupation&quot;: &quot;TV Actress&quot;,\r\n  &quot;photo_url&quot;: &quot;http://www.famousbirthdays.com/thumbnails/cates-challen-medium.jpg&quot;\r\n}]`, `34919846520671040000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-json line-numbers\"><code class=\"language-json\"><span class=\"token punctuation\">[</span><span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">\"age\"</span><span class=\"token operator\">:</span> <span class=\"token number\">18</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_place\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Harij, Gujarat, India\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_sign\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Pisces\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_year\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1996\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birthday\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"September 28\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Daxeel Soni\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"occupation\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Developer of this API. More: www.daxeelsoni.in\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"photo_url\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"http://daxeelsoni.in/images/me.jpg\"</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">\"age\"</span><span class=\"token operator\">:</span> <span class=\"token number\">28</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_place\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Houston\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_sign\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Libra\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_year\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1987\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birthday\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"September 28\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Hilary Duff\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"occupation\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"TV Actress\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"photo_url\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"http://www.famousbirthdays.com/thumbnails/duff-hilary-medium.jpg\"</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">\"age\"</span><span class=\"token operator\">:</span> <span class=\"token number\">38</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_place\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Columbia\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_sign\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Libra\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_year\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1977\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birthday\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"September 28\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Young Jeezy\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"occupation\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Rapper\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"photo_url\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"http://www.famousbirthdays.com/thumbnails/jeezy-young-medium.jpg\"</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">\"age\"</span><span class=\"token operator\">:</span> <span class=\"token number\">48</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_place\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Roanoke\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_sign\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Libra\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birth_year\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1967\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"birthday\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"September 28\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Challen Cates\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"occupation\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"TV Actress\"</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token property\">\"photo_url\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"http://www.famousbirthdays.com/thumbnails/cates-challen-medium.jpg\"</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Now that we have our JSON file, let’s seed the database. Replace the initial connect code with this:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"51705009579308080000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`app.listen(5000, () => {\r\n  MongoClient.connect(\r\n    CONNECTION_URL,\r\n    {\r\n      useNewUrlParser: true,\r\n      sslValidate: false,\r\n    },\r\n    (error, client) => {\r\n      if (error) {\r\n        throw error;\r\n      }\r\n      database = client.db(DATABASE_NAME);\r\n      collection = database.collection(&quot;celebrities&quot;);\r\n      collection.estimatedDocumentCount({}, (erorr, numOfRecords) => {\r\n        if (numOfRecords <= 0) {\r\n          fs.readFile(&quot;./info.json&quot;, &quot;utf8&quot;, (err, data) => {\r\n            if (err) {\r\n              console.log(\\`Error reading file from disk: \\${err}\\`);\r\n            } else {\r\n              // parse JSON string to JSON object\r\n              const celebrities = JSON.parse(data);\r\n\r\n              collection.insertMany(celebrities, (error, result) => {\r\n                if (error) {\r\n                  console.log(\\`Error in saving seed data: \\${error}\\`);\r\n                }\r\n\r\n                console.log(\\`Seed data inserted into the database!\\`);\r\n              });\r\n            }\r\n          });\r\n        } else {\r\n          console.log(\\`Connected to database\\`);\r\n        }\r\n      });\r\n    }\r\n  );\r\n});`, `51705009579308080000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\">app<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token number\">5000</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  MongoClient<span class=\"token punctuation\">.</span><span class=\"token function\">connect</span><span class=\"token punctuation\">(</span>\r\n    <span class=\"token constant\">CONNECTION_URL</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token punctuation\">{</span>\r\n      <span class=\"token literal-property property\">useNewUrlParser</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span>\r\n      <span class=\"token literal-property property\">sslValidate</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> client</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token keyword\">throw</span> error<span class=\"token punctuation\">;</span>\r\n      <span class=\"token punctuation\">}</span>\r\n      database <span class=\"token operator\">=</span> client<span class=\"token punctuation\">.</span><span class=\"token function\">db</span><span class=\"token punctuation\">(</span><span class=\"token constant\">DATABASE_NAME</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n      collection <span class=\"token operator\">=</span> database<span class=\"token punctuation\">.</span><span class=\"token function\">collection</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"celebrities\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n      collection<span class=\"token punctuation\">.</span><span class=\"token function\">estimatedDocumentCount</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">erorr<span class=\"token punctuation\">,</span> numOfRecords</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>numOfRecords <span class=\"token operator\">&lt;=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n          fs<span class=\"token punctuation\">.</span><span class=\"token function\">readFile</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"./info.json\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"utf8\"</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">err<span class=\"token punctuation\">,</span> data</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n            <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n              console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Error reading file from disk: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>err<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n            <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n              <span class=\"token comment\">// parse JSON string to JSON object</span>\r\n              <span class=\"token keyword\">const</span> celebrities <span class=\"token operator\">=</span> <span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">parse</span><span class=\"token punctuation\">(</span>data<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n              collection<span class=\"token punctuation\">.</span><span class=\"token function\">insertMany</span><span class=\"token punctuation\">(</span>celebrities<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> result</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n                <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n                  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Error in saving seed data: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>error<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n                <span class=\"token punctuation\">}</span>\r\n\r\n                console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Seed data inserted into the database!</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n              <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n            <span class=\"token punctuation\">}</span>\r\n          <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n          console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Connected to database</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span>\r\n      <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span>\r\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>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.</p>\n<p>Don’t forget to add <code class=\"language-text\">const fs = require('fs')</code> at the top with other require statements.</p>\n<p>Now if you run <code class=\"language-text\">npm start</code> would should see the application start and then the <strong>seed data inserted</strong> message.</p>\n<h3 id=\"creating-the-endpoint\" style=\"position:relative;\"><a href=\"#creating-the-endpoint\" aria-label=\"creating the endpoint permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Creating the endpoint</h3>\n<p>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:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"98512496102421050000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`app.get(&quot;/api/celebrities&quot;, (request, response) => {\r\n    collection.find({}).toArray((error, result) => {\r\n        if(error) {\r\n            return response.status(500).send(error);\r\n        }\r\n        response.send(result);\r\n    });\r\n});`, `98512496102421050000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\">app<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"/api/celebrities\"</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">request<span class=\"token punctuation\">,</span> response</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    collection<span class=\"token punctuation\">.</span><span class=\"token function\">find</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">toArray</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> result</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n            <span class=\"token keyword\">return</span> response<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">500</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span>\r\n        response<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span>result<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"testing-the-application\" style=\"position:relative;\"><a href=\"#testing-the-application\" aria-label=\"testing the application permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Testing the application</h2>\n<p>You’re now ready to get your celebrities via the API. Simply open a browser and head over to <code class=\"language-text\">http://localhost:5000/api/celebrities</code> or just use the below command:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"66009006676875100000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`curl -X GET http://localhost:5000/api/celebrities`, `66009006676875100000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-bash line-numbers\"><code class=\"language-bash\"><span class=\"token function\">curl</span> <span class=\"token parameter variable\">-X</span> GET http://localhost:5000/api/celebrities</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>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 😊).</p>\n<p>Enjoy hacking and let us know what awesome projects you’re doing using the emulator.</p>\n<p>PS: You can find the finished code in my <a href=\"https://github.com/yashints/azure-cosmosdb-emulator-mongodb\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GitHub repository here</a>.</p>","timeToRead":5,"frontmatter":{"unformattedDate":"2020-11-30T00:00:00.000Z","date":"November 30, 2020","title":"How to use Azure Cosmos Emulator as a local MongoDb database 🌍","subtitle":null,"id":null,"author":"Yaser Adel Mehraban","gravatar":null,"canonical_url":null,"tags":["azure","cosmosdb","emulator","mongodb"],"thumbnail":null,"cover":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB9klEQVR42h2M226bQBRF+e2qv9DHSr19QlU1VWxFTZSYuIkHm8FOjGODDQy3sWGAGe6GqZ0WIi0tbR3tfYSlQ3VSb0mthxVKapcevTccerSTHpw2Qd7us7YPb/ZYs08bFGbC/FCoOHty6eqQqzg1wsIkOYpKFFcGKcyo7h3mbpjjpHKTCkXF7pD6tMasEaZBea1ag8lyrFqjuT4cK9+vRld/5F+34OfNw+UIXN6BK3H65VocQlVeo9kGifLq2cB+2grATxeUP9O/Wsyd/J/qxqKyvpupd3ANVPN+rt3IK2lpDBwyI9W+evWKk1eezey8DkoB4BQGtWwzaLGFm2/Co52d/OLc9cJjj1+9usUZZyeb8V3SIsa3SbOJ+erQjc1IsZi8oxCl0Mmhnc/tfOkXeng04qZjGzVa3GqMa5RrCe9mW9ZuGVf7sRYuEFuYiYKymUknOploRDJT6JayU0Cn7ALEFSSNknAl4jBogNUsw25cCJLJZo/g6ffF3GLiszuYvAynmviCFa+CXgn9sndPAXG9iBvxpXn/7vTtgqssF6CdDW/Bp4+fJSMCOyKhZOakE4PIdqbgSvFLxSv7sO9ytQjrB9R++Mp/PLYQM+Fh5U/06H5DJCMGHWYCEJMMImkHSQ+kTecQWN2RSoh2he47xIns0PE6+A9uGgnWVblcLwAAAABJRU5ErkJggg=="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/80ca7cfe3fe0e3b096cee52649271d28/a2195/socialpreview.png","srcSet":"/static/80ca7cfe3fe0e3b096cee52649271d28/cadb4/socialpreview.png 750w,\n/static/80ca7cfe3fe0e3b096cee52649271d28/a2195/socialpreview.png 800w","sizes":"(min-width: 800px) 800px, 100vw"},"sources":[{"srcSet":"/static/80ca7cfe3fe0e3b096cee52649271d28/11ba1/socialpreview.webp 750w,\n/static/80ca7cfe3fe0e3b096cee52649271d28/5f042/socialpreview.webp 800w","type":"image/webp","sizes":"(min-width: 800px) 800px, 100vw"}]},"width":800,"height":400}}},"img":null,"socialPreview":{"publicURL":"/static/80ca7cfe3fe0e3b096cee52649271d28/socialpreview.png"}}}},"pageContext":{"title":"How to use Azure Cosmos Emulator as a local MongoDb database 🌍","postPath":"/blog/2020/11/30/az-cosmos-emulator","nextPost":"/blog/2020/12/07/dual-screen","previousPost":"/blog/2020/11/13/copy-across-devices"}},"staticQueryHashes":[],"slicesMap":{}}