Azure Cosmos DB is Microsoft's globally distributed multi-model database service. You can communicate with the Azure Cosmos DB's API for MongoDB using any of the open-source MongoDB client drivers. The Azure Cosmos DB's API for MongoDB enables the use of existing client drivers by adhering to the MongoDB wire protocol.
By using the Azure Cosmos DB's API for MongoDB, you can enjoy the benefits of the MongoDB you're used to, with all of the enterprise capabilities that Cosmos DB provides: global distribution, automatic sharding, availability and latency guarantees, encryption at rest, backups, and much more.
Protocol Support
The Azure Cosmos DB's API for MongoDB is compatible with MongoDB server version 3.6 by default for new accounts. The supported operators and any limitations or exceptions are listed below. Any client driver that understands these protocols should be able to connect to Azure Cosmos DB's API for MongoDB. Note that when using Azure Cosmos DB's API for MongoDB accounts, the 3.6 version of account has the endpoint in the format *.mongo.cosmos.azure.com whereas the 3.2 version of account has the endpoint in the format *.documents.azure.com.
Query language support
Azure Cosmos DB's API for MongoDB provides comprehensive support for MongoDB query language constructs. The following sections show the detailed list of server operations, operators, stages, commands, and options currently supported by Azure Cosmos DB.
Note
This article only lists the supported server commands and excludes client-side wrapper functions. Client-side wrapper functions such as deleteMany() and updateMany() internally utilize the delete() and update() server commands. Functions utilizing supported server commands are compatible with Azure Cosmos DB's API for MongoDB.
Database commands
Azure Cosmos DB's API for MongoDB supports the following database commands:
In the $regex queries, left-anchored expressions allow index search. However, using 'i' modifier (case-insensitivity) and 'm' modifier (multiline) causes the collection scan in all expressions.
When there's a need to include '$' or '|', it is best to create two (or more) regex queries. For example, given the following original query: find({x:{$regex: /^abc$/}), it has to be modified as follows:
find({x:{$regex: /^abc/, x:{$regex:/^abc$/}}).
The first part will use the index to restrict the search to those documents beginning with ^abc and the second part will match the exact entries. The bar operator '|' acts as an "or" function - the query find({x:{$regex: /^abc|^def/}) matches the documents in which field 'x' has values that begin with "abc" or "def". To utilize the index, it's recommended to break the query into two different queries joined by the $or operator: find( {$or : [{x: $regex: /^abc/}, {$regex: /^def/}] }).
Array operators
Command
Supported
$all
Yes
$elemMatch
Yes
$size
Yes
Comment operator
Command
Supported
$comment
Yes
Projection operators
Command
Supported
$elemMatch
Yes
$meta
No
$slice
Yes
Update operators
Field update operators
Command
Supported
$inc
Yes
$mul
Yes
$rename
Yes
$setOnInsert
Yes
$set
Yes
$unset
Yes
$min
Yes
$max
Yes
$currentDate
Yes
Array update operators
Command
Supported
$
Yes
$[]
Yes
$[]
Yes
$addToSet
Yes
$pop
Yes
$pullAll
Yes
$pull
Yes
$push
Yes
$pushAll
Yes
Update modifiers
Command
Supported
$each
Yes
$slice
Yes
$sort
Yes
$position
Yes
Bitwise update operator
Command
Supported
$bit
Yes
$bitsAllSet
No
$bitsAnySet
No
$bitsAllClear
No
$bitsAnyClear
No
Geospatial operators
Operator
Supported
$geoWithin
Yes
$geoIntersects
Yes
$near
Yes
$nearSphere
Yes
$geometry
Yes
$minDistance
Yes
$maxDistance
Yes
$center
No
$centerSphere
No
$box
No
$polygon
No
Cursor methods
Command
Supported
cursor.batchSize()
Yes
cursor.close()
Yes
cursor.isClosed()
Yes
cursor.collation()
No
cursor.comment()
Yes
cursor.count()
Yes
cursor.explain()
No
cursor.forEach()
Yes
cursor.hasNext()
Yes
cursor.hint()
Yes
cursor.isExhausted()
Yes
cursor.itcount()
Yes
cursor.limit()
Yes
cursor.map()
Yes
cursor.maxScan()
Yes
cursor.maxTimeMS()
Yes
cursor.max()
Yes
cursor.min()
Yes
cursor.next()
Yes
cursor.noCursorTimeout()
No
cursor.objsLeftInBatch()
Yes
cursor.pretty()
Yes
cursor.readConcern()
Yes
cursor.readPref()
Yes
cursor.returnKey()
No
cursor.showRecordId()
No
cursor.size()
Yes
cursor.skip()
Yes
cursor.sort()
Yes
cursor.tailable()
No
cursor.toArray()
Yes
Sort operations
When using the findOneAndUpdate operation, sort operations on a single field are supported but sort operations on multiple fields are not supported.
Unique indexes
Unique indexes ensure that a specific field doesn't have duplicate values across all documents in a collection, similar to the way uniqueness is preserved on the default "_id" key. You can create unique indexes in Cosmos DB by using the createIndex command with the unique constraint parameter:
Compound indexes provide a way to create an index for groups of fields for up to 8 fields. This type of index differs from the native MongoDB compound indexes. In Azure Cosmos DB, compound indexes are used for sorting operations that are applied to multiple fields. To create a compound index you need to specify more than one property as the parameter:
Cosmos DB supports a time-to-live (TTL) based on the timestamp of the document. TTL can be enabled for collections by going to the Azure portal.
User and role management
Cosmos DB does not yet support users and roles. However, Cosmos DB supports Azure role-based access control (Azure RBAC) and read-write and read-only passwords/keys that can be obtained through the Azure portal (Connection String page).
Replication
Cosmos DB supports automatic, native replication at the lowest layers. This logic is extended out to achieve low-latency, global replication as well. Cosmos DB does not support manual replication commands.
Write Concern
Some applications rely on a Write Concern which specifies the number of responses required during a write operation. Due to how Cosmos DB handles replication in the background all writes are all automatically Quorum by default. Any write concern specified by the client code is ignored. Learn more in Using consistency levels to maximize availability and performance.
Sharding
Azure Cosmos DB supports automatic, server-side sharding. It manages shard creation, placement, and balancing automatically. Azure Cosmos DB does not support manual sharding commands, which means you don't have to invoke commands such as addShard, balancerStart, moveChunk etc. You only need to specify the shard key while creating the containers or querying the data.
Sessions
Azure Cosmos DB does not yet support server-side sessions commands.
Learn how to use Studio 3T with Azure Cosmos DB's API for MongoDB.
Learn how to use Robo 3T with Azure Cosmos DB's API for MongoDB.
Explore MongoDB samples with Azure Cosmos DB's API for MongoDB.
Note: This article describes a feature of Azure Cosmos DB that provides wire protocol compatibility with MongoDB databases. Microsoft does not run MongoDB databases to provide this service. Azure Cosmos DB is not affiliated with MongoDB, Inc.