0) { response.setBody(docs[0]); } else { var accepted = collection.createDocument(collection.getSelfLink(), body, function(err, doc) { if (err) throw err; response.setBody(doc); }); if (!accepted) throw new Error("createDocument not accepted"); } }); if (!isAccepted) throw new Error("queryDocuments not accepted"); }; await container.scripts.storedProcedures.create({ id: "createIfAbsent", body: sprocBody }); // Execute it const result = await container.scripts.storedProcedure("createIfAbsent").execute(partitionKey, [id, body]); Key limitations to know for interviews: Single partition scope only — A stored procedure cannot touch items across multiple partition key values. All reads and writes must target the same logical partition passed at invocation time. Continuation token required for large result sets — Stored procedures have a time and response size budget. If a bulk operation does not complete in one execution, you must implement pagination using continuation tokens and re-invoke the procedure. No external HTTP calls — Server-side scripts cannot call external APIs or Azure services. Logic must be pure JS within the Cosmos DB context API. JavaScript only — No other language is supported for stored procedures, triggers, or user-defined functions."> 0) { response.setBody(docs[0]); } else { var accepted = collection.createDocument(collection.getSelfLink(), body, function(err, doc) { if (err) throw err; response.setBody(doc); }); if (!accepted) throw new Error("createDocument not accepted"); } }); if (!isAccepted) throw new Error("queryDocuments not accepted"); }; await container.scripts.storedProcedures.create({ id: "createIfAbsent", body: sprocBody }); // Execute it const result = await container.scripts.storedProcedure("createIfAbsent").execute(partitionKey, [id, body]); Key limitations to know for interviews: Single partition scope only — A stored procedure cannot touch items across multiple partition key values. All reads and writes must target the same logical partition passed at invocation time. Continuation token required for large result sets — Stored procedures have a time and response size budget. If a bulk operation does not complete in one execution, you must implement pagination using continuation tokens and re-invoke the procedure. No external HTTP calls — Server-side scripts cannot call external APIs or Azure services. Logic must be pure JS within the Cosmos DB context API. JavaScript only — No other language is supported for stored procedures, triggers, or user-defined functions." />

Prev Next

Database / Azure Cosmos DB interview questions

What is a stored procedure in Cosmos DB and what are its limitations?

Stored procedures in Cosmos DB are JavaScript functions registered on a container and executed server-side on the Cosmos DB engine itself. They run atomically within a single partition — meaning all operations inside the stored procedure either all commit or all roll back, giving you ACID transaction semantics without distributed coordination overhead.

A stored procedure is registered and then called via the SDK or REST API:

// Register a stored procedure
const sprocBody = function createDocIfAbsent(id, body) {
  var context = getContext();
  var collection = context.getCollection();
  var response = context.getResponse();

  var query = { query: "SELECT * FROM c WHERE c.id = @id", parameters: [{ name: "@id", value: id }] };
  var isAccepted = collection.queryDocuments(collection.getSelfLink(), query, {}, function(err, docs) {
    if (err) throw err;
    if (docs.length > 0) {
      response.setBody(docs[0]);
    } else {
      var accepted = collection.createDocument(collection.getSelfLink(), body, function(err, doc) {
        if (err) throw err;
        response.setBody(doc);
      });
      if (!accepted) throw new Error("createDocument not accepted");
    }
  });
  if (!isAccepted) throw new Error("queryDocuments not accepted");
};

await container.scripts.storedProcedures.create({ id: "createIfAbsent", body: sprocBody });

// Execute it
const result = await container.scripts.storedProcedure("createIfAbsent").execute(partitionKey, [id, body]);

Key limitations to know for interviews:

  • Single partition scope only — A stored procedure cannot touch items across multiple partition key values. All reads and writes must target the same logical partition passed at invocation time.
  • Continuation token required for large result sets — Stored procedures have a time and response size budget. If a bulk operation does not complete in one execution, you must implement pagination using continuation tokens and re-invoke the procedure.
  • No external HTTP calls — Server-side scripts cannot call external APIs or Azure services. Logic must be pure JS within the Cosmos DB context API.
  • JavaScript only — No other language is supported for stored procedures, triggers, or user-defined functions.
Across how many partition key values can a single Cosmos DB stored procedure operate?
What language must Cosmos DB stored procedures and triggers be written in?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Azure Cosmos DB and what problems does it solve? What are the different APIs available in Azure Cosmos DB? What is a partition key in Azure Cosmos DB and why is choosing it correctly so important? What are Request Units (RU/s) in Azure Cosmos DB? What are the five consistency levels in Azure Cosmos DB? How does global distribution work in Azure Cosmos DB? What is the Cosmos DB Change Feed and what are its main use cases? What is provisioned throughput vs autoscale vs serverless in Cosmos DB? How does indexing work in Azure Cosmos DB? What is Time to Live (TTL) in Cosmos DB and how do you configure it? What is a stored procedure in Cosmos DB and what are its limitations? What is the difference between a point read and a query in Cosmos DB? What is the Cosmos DB NoSQL query language and how does it differ from standard SQL? What is the Cosmos DB transactional batch API? What is Cosmos DB Integrated Cache and how does it reduce RU consumption? How does optimistic concurrency work in Azure Cosmos DB? What is hierarchical partition keys in Cosmos DB and when do you use it? What is the Cosmos DB Bulk Executor and how do you use bulk operations in the SDK? What are Cosmos DB triggers and user-defined functions (UDFs)? How does Cosmos DB handle conflicts in multi-region write (multi-master) setups? What is the Cosmos DB Emulator and how is it used in development? What is Cosmos DB for MongoDB API and what version compatibility does it provide? What is the Cosmos DB analytical store and Azure Synapse Link? What are Cosmos DB materialized views and how do they differ from containers? How does Cosmos DB pricing work and what are the key cost drivers? What is the Cosmos DB Gremlin API and what is it optimized for? How does Cosmos DB backup and restore work? What is the Cosmos DB Patch API and how does it differ from Replace? What is the Cosmos DB Cassandra API and how does CQL map to Cosmos DB concepts? How do you model one-to-many relationships in Cosmos DB? What is the Cosmos DB free tier and what does it include? What is the Cosmos DB SDK and what are the key client configuration options? What is the Cosmos DB Table API and when would you migrate from Azure Table Storage to it? How does Cosmos DB handle security and access control?
Show more question and Answers...

MuleESB

Comments & Discussions