{"id":9088,"date":"2024-11-19T05:30:31","date_gmt":"2024-11-19T13:30:31","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=9088"},"modified":"2024-11-15T10:32:59","modified_gmt":"2024-11-15T18:32:59","slug":"change-streams-in-public-preview-for-vcore-based-azure-cosmos-db-for-mongodb","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/change-streams-in-public-preview-for-vcore-based-azure-cosmos-db-for-mongodb\/","title":{"rendered":"Change Streams in public preview for vCore-based Azure Cosmos DB for MongoDB"},"content":{"rendered":"<p>Change streams provide a real-time flow of database changes from your database to your application. This feature enables the development of reactive applications by allowing you to subscribe to database changes.<\/p>\n<h3>Configuring Change Streams<\/h3>\n<p>To get started with change streams, let&#8217;s look at an example code snippet that initiates a change stream on the <code>exampleCollection<\/code> collection. This script continuously monitors for any changes and prints the change event in JSON format.<\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none\"><code class=\"language-js\"><\/code><\/div>\n<div>\n<pre class=\"prettyprint language-js\"><code class=\"language-js\">\/\/ Open a change stream\r\nconst changeStream = db.exampleCollection.watch();\r\n\r\n\/\/ Listen for changes\r\nwhile (changeStream.hasNext()) {\r\n    const change = changeStream.next();\r\n    printjson(change);\r\n}\r\n\r\n<\/code><\/pre>\n<h3>Personalise stream with pipeline stages<\/h3>\n<p>You can customize your change stream output by specifying an array of one or more pipeline stages during configuration. Supported operators include:<\/p>\n<ul>\n<li><code>$addFields<\/code><\/li>\n<li><code>$match<\/code><\/li>\n<li><code>$project<\/code><\/li>\n<li><code>$set<\/code><\/li>\n<li><code>$unset<\/code><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<h3>Use-cases<\/h3>\n<ul>\n<li><strong>Real-time Analytics<\/strong>: Monitor and analyze data changes as they happen to provide up-to-date insights and dashboards.<\/li>\n<li><strong>Event-Driven Architectures<\/strong>: Trigger actions or workflows in response to specific database changes, enabling reactive application development.<\/li>\n<li><strong>Audit Logging<\/strong>: Keep track of all changes made to your database for compliance and security purposes.<\/li>\n<li><strong>Monitoring and Alerts<\/strong>: Set up real-time alerts for specific changes or anomalies in your data, enhancing monitoring capabilities<\/li>\n<\/ul>\n<h2><span style=\"font-size: 18pt;\"><strong><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/11\/CS_usecase.png\"><img decoding=\"async\" class=\"alignnone size-medium wp-image-9095\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/11\/CS_usecase-300x94.png\" alt=\"Image CS usecase\" width=\"300\" height=\"94\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/11\/CS_usecase-300x94.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/11\/CS_usecase-1024x319.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/11\/CS_usecase-768x240.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/11\/CS_usecase-1536x479.png 1536w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/11\/CS_usecase-2048x639.png 2048w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/strong><\/span><\/h2>\n<h2>Conclusion<\/h2>\n<p>Change streams provide a powerful way to monitor real-time database changes, enabling the development of highly responsive applications. Despite some limitations, the feature&#8217;s ability to push changes to your application as they happen can significantly streamline your data handling processes. Please follow the official <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/mongodb\/vcore\/change-streams?tabs=javascript%2CInsert\">documentation<\/a> page for learning more.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>Leave a review<\/strong><\/span><\/h2>\n<p>Tell us about your Azure Cosmos DB experience! Leave a review on PeerSpot and we\u2019ll gift you $50. <a href=\"https:\/\/peerspotdotcom.my.site.com\/proReviews\/?SalesOpportunityProduct=00kPy000004TKXJIA4&amp;productPeerspotNumber=30881&amp;CalendlyAccount=peerspot&amp;CalendlyFormLink=peerspot-product-reviews-ps-gc-vi-sf-50&amp;giftCard=50\" target=\"_blank\" rel=\"noopener\">Get started here.<\/a><\/p>\n<h2>About Azure Cosmos DB<\/h2>\n<p>Azure Cosmos DB is a fully managed and serverless distributed database for modern app development, with SLA-backed speed and availability, automatic and instant scalability, and support for open-source PostgreSQL, MongoDB, and Apache Cassandra.\u00a0<a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\">Try Azure Cosmos DB for free here.<\/a>\u00a0To stay in the loop on Azure Cosmos DB updates, follow us on\u00a0<a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\">X<\/a>,\u00a0<a href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>, and\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\">LinkedIn<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Change streams provide a real-time flow of database changes from your database to your application. This feature enables the development of reactive applications by allowing you to subscribe to database changes. Configuring Change Streams To get started with change streams, let&#8217;s look at an example code snippet that initiates a change stream on the exampleCollection [&hellip;]<\/p>\n","protected":false},"author":175387,"featured_media":9109,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-9088","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api"],"acf":[],"blog_post_summary":"<p>Change streams provide a real-time flow of database changes from your database to your application. This feature enables the development of reactive applications by allowing you to subscribe to database changes. Configuring Change Streams To get started with change streams, let&#8217;s look at an example code snippet that initiates a change stream on the exampleCollection [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/9088","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\/175387"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=9088"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/9088\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/9109"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=9088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=9088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=9088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}