page_type | languages | products | description | urlFragment | |||
---|---|---|---|---|---|---|---|
sample |
|
|
Azure Cosmos DB is a fully managed globally distributed, multi-model database service, transparently replicating your data across any number of Azure regions. |
azure-cosmos-db-mongodb-mongoose-geo-readpreference |
Azure Cosmos DB is a fully managed globally distributed, multi-model database service, transparently replicating your data across any number of Azure regions. You can elastically scale throughput and storage, and take advantage of fast, single-digit-millisecond data access using the API of your choice backed by 99.999 SLA. This sample shows you how to use ReadPreference command against Azure Cosmos DB for MongoDB API from a Mongoose framework app.
-
Before you can run this sample, you must have the following prerequisites:
- An active Azure account. If you don't have one, you can sign up for a free account. Alternatively, you can use the Azure Cosmos DB Emulator for this tutorial.
- Node JS installation
-
Then, clone this repository.
-
Next, substitute the
remoteUrl
in config/database.js with your Cosmos DB account connection string. -
npm install
to install dependencies (mongo driver) -
npm start
-
The application starts at
http://localhost:8080
. Visit this Url to access the application.
The code included in this sample is intended to illustrate using ReadPreference and tags against an Azure Cosmos DB for MongoDB API account for performing reads across geo replicated read regions. This application is a fork of this TODO application and modified to add read preference setting.
- Refer
getTodos
method inapp\routes.js
for setting Read preference. Find the snippet below:
Todo.find(function (err, todos) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err) {
res.send(err);
}
res.json(todos); // return all todos in JSON format
}).read('secondary');