{"id":3913,"date":"2022-01-06T10:15:01","date_gmt":"2022-01-06T18:15:01","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=3913"},"modified":"2022-01-06T10:23:27","modified_gmt":"2022-01-06T18:23:27","slug":"build-a-node-js-app-with-azure-cosmos-dbs-api-for-mongodb","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/build-a-node-js-app-with-azure-cosmos-dbs-api-for-mongodb\/","title":{"rendered":"Build a Node.js app with Azure Cosmos DB\u2019s API for MongoDB"},"content":{"rendered":"<p><span data-contrast=\"auto\">A new video series on<\/span><span data-contrast=\"auto\">\u00a0using Node.js with Azure Cosmos DB API for MongoDB is now available on <\/span><a href=\"https:\/\/youtube.com\/playlist?list=PLmamF3YkHLoKdpcn2nzCc7-eTXIYVGiZM\"><span data-contrast=\"none\">the Azure Cosmos DB YouTube channel<\/span><\/a><span data-contrast=\"auto\">. The series walks through how we built the backend functionalities of a demo application <a href=\"https:\/\/aka.ms\/cosmosbookstore\">&#8220;Cosmos Bookstore&#8221;. <\/a>The application is a books catalog that uses React for the front-end layer, NodeJS and Express for the back-end layer, and Azure Cosmos DB (with the API for MongoDB) as the database. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><figure id=\"attachment_3926\" aria-labelledby=\"figcaption_attachment_3926\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/01\/Cosmos-Bookstore-screenshot-1.jpg\"><img decoding=\"async\" class=\"size-large wp-image-3926\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/01\/Cosmos-Bookstore-screenshot-1-1024x519.jpg\" alt=\"screenshot of the Cosmos Books demo application showing seven book covers in two rows\" width=\"640\" height=\"324\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/01\/Cosmos-Bookstore-screenshot-1-1024x519.jpg 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/01\/Cosmos-Bookstore-screenshot-1-300x152.jpg 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/01\/Cosmos-Bookstore-screenshot-1-768x389.jpg 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/01\/Cosmos-Bookstore-screenshot-1-1536x778.jpg 1536w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/01\/Cosmos-Bookstore-screenshot-1.jpg 1912w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"figcaption_attachment_3926\" class=\"wp-caption-text\">Cosmos Bookstore demo application<\/figcaption><\/figure><\/p>\n<p>&nbsp;<\/p>\n<p><span data-contrast=\"auto\">If you are a Node.js developer who is new to <a href=\"https:\/\/azure.microsoft.com\/services\/cosmos-db\/\" target=\"_blank\" rel=\"noopener\">Azure Cosmos DB<\/a> or its <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/mongodb\/mongodb-introduction\" target=\"_blank\" rel=\"noopener\">API for MongoDB<\/a>, this series is for you. <\/span><span style=\"font-size: 1rem;\" data-contrast=\"auto\">While building the demo app, you\u2019ll learn about:<\/span><span style=\"font-size: 1rem;\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Connecting to the database &amp; the client configuration<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"none\">Reads &amp; Queries<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><span data-contrast=\"none\">Sorting &amp; Indexing<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"4\" data-aria-level=\"1\"><span data-contrast=\"none\">Updates<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Using different operators<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"none\">Checking RU cost of the previous operation<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><span data-contrast=\"none\">Regex queries<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"4\" data-aria-level=\"1\"><span data-contrast=\"none\">Aggregation pipelines<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"5\" data-aria-level=\"1\"><span data-contrast=\"none\">Azure Search integration<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<\/ul>\n<p><span data-contrast=\"auto\">\u202f<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\"> T<\/span><span data-contrast=\"auto\">he series has five videos: <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<h4><a href=\"https:\/\/youtu.be\/45datq39xNo\"><span data-contrast=\"none\">Part 1\u202f- Connecting &amp; Reading<\/span><\/a><span data-contrast=\"auto\">\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/h4>\n<p><span data-contrast=\"auto\">This introduction talks about the application stack, the sample dataset used, and its schema. We will walk through the code structure and the backend modules, then initiate a common database client that gets used by all the backend modules. We then demonstrate the usage of MongoDB find(), cursor for reading the list of books from the database. Post this, we achieve pagination with skip() and limit(). Lastly, we demonstrate the usage of projection, indexing, and sorting.<\/span><\/p>\n<p><iframe title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/45datq39xNo\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<h4><a href=\"https:\/\/youtu.be\/62TKll2wQjI\"><span data-contrast=\"none\">Part 2 &#8211; Query conditions &amp; Updates<\/span><\/a><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/h4>\n<p><span data-contrast=\"auto\">In this video, we use different MongoDB query conditions and operators to allow filtering options in the book catalog. We demonstrate the use of the $gt operator for comparison, in order to filter books by rating. We use $in array expression to filter books by their book format. findOne() command is used to get the book details. Lastly, we demonstrate the use of updateOne() command with array operators $push, $unset and $pull to add or remove comments on a book.<\/span><\/p>\n<p><iframe title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/62TKll2wQjI\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><span data-contrast=\"auto\">\u202f<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<h4><a href=\"https:\/\/youtu.be\/T8lY_BCOFTg\"><span data-contrast=\"none\">Part 3 &#8211; Regex, Aggregation pipeline &amp; Request charge<\/span><\/a><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/h4>\n<p><span data-contrast=\"auto\">In this part, we focus on slightly more advanced use cases. We demonstrate the use of regex and aggregation pipeline to achieve typeahead search in the genres filter of the books catalog. We use $unwind, $match and $group stages in the aggregation pipeline. Lastly, we demonstrate how to check the request charge of the last operation. This is an explicit functionality in the Azure Cosmos DB API for MongoDB.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><iframe title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/T8lY_BCOFTg\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><span data-contrast=\"auto\">\u202f<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<h4><a href=\"https:\/\/youtu.be\/16huduth7uQ\"><span data-contrast=\"none\">Part 4 &#8211; Azure Search integration &#8211; Setup &amp; text search<\/span><\/a><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/h4>\n<p><span data-contrast=\"auto\">Text search functionality in the Azure Cosmos DB API for MongoDB must be achieved in integration with Azure Cognitive Search. This part walks through how to set up the search service, import data and create the search index. We then use the search index to provide basic text search functionality in the application.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><iframe title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/16huduth7uQ\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><span data-contrast=\"auto\">\u202f<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<h4><a href=\"https:\/\/youtu.be\/mwTrmjOlrN8\"><span data-contrast=\"none\">Part 5 &#8211; Azure Search integration &#8211; Search options<\/span><\/a><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/h4>\n<p><span data-contrast=\"auto\">This last part covers advanced search functionalities. This includes pagination with text search result, as well as select and orderby. We also demonstrate filtering using Azure search operators like gt, search.in and search.ismatch. This allows the filters and sort options available in the application to be functional against the search indexer data too.<\/span><\/p>\n<p><iframe title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/mwTrmjOlrN8\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><span data-contrast=\"auto\">We hope you find this video series helpful. Drop a comment below if you have ideas for other scenarios and capabilities to add to this demo application.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<h3 id=\"about-azure-cosmos-db\">About Azure Cosmos DB<i class=\"fabric-icon fabric-icon--Link\" aria-hidden=\"true\"><\/i><\/h3>\n<p><a href=\"https:\/\/azure.microsoft.com\/services\/cosmos-db\/\" target=\"_blank\" rel=\"noopener\">Azure Cosmos DB<\/a>\u00a0is a fast and scalable distributed NoSQL database, built for modern application development. Get guaranteed single-digit millisecond response times and 99.999-percent availability,\u00a0<a href=\"https:\/\/azure.microsoft.com\/support\/legal\/sla\/cosmos-db\/\" target=\"_blank\" rel=\"noopener\" data-bi-an=\"content-overview-01\" data-bi-tn=\"undefined\">backed by SLAs<\/a>,\u00a0<a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/scaling-throughput\" target=\"_blank\" rel=\"noopener\" data-bi-an=\"content-overview-01\" data-bi-tn=\"undefined\">automatic and instant scalability<\/a>, and open-source APIs for MongoDB and Cassandra. Enjoy fast writes and reads anywhere in the world with turnkey data replication and multi-region writes.<\/p>\n<p class=\"\">Watch our <a href=\"https:\/\/youtube.com\/playlist?list=PLmamF3YkHLoLLGUtSoxmUkORcWaTyHlXp\" target=\"_blank\" rel=\"noopener\">Get Started videos<\/a> on YouTube and explore ways to <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/optimize-dev-test\" target=\"_blank\" rel=\"noopener\">dev\/test free.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Build a Node.JS application with Azure Cosmos DB using the API for MongoDB, React, and Express. A new five-part developer video series demonstrates key steps and best practices to help you get started. <\/p>\n","protected":false},"author":79038,"featured_media":3926,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[15],"tags":[],"class_list":["post-3913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb-api"],"acf":[],"blog_post_summary":"<p>Build a Node.JS application with Azure Cosmos DB using the API for MongoDB, React, and Express. A new five-part developer video series demonstrates key steps and best practices to help you get started. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/3913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/79038"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=3913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/3913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/3926"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=3913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=3913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=3913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}