title | description | services | author | manager | editor | documentationcenter | ms.assetid | ms.service | ms.workload | ms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Azure Cosmos DB feature support for MongoDB | Microsoft Docs |
Learn about the feature support the Azure Cosmos DB MongoDB API provides for MongoDB 3.4. |
cosmos-db |
alekseys |
jhubbard |
29b6547c-3201-44b6-9e0b-e6f56e473e24 |
cosmos-db |
data-services |
na |
na |
article |
11/15/2017 |
alekseys |
Azure Cosmos DB is Microsoft's globally distributed multi-model database service. You can communicate with the database's MongoDB API through any of the open source MongoDB client drivers. The MongoDB API enables the use of existing client drivers by adhering to the MongoDB wire protocol.
By using the Azure Cosmos DB MongoDB API, you can enjoy the benefits of the MongoDB APIs you're used to, with all of Azure Cosmos DB's enterprise capabilities: global distribution, automatic sharding, availability and latency guarantees, automatic indexing of every field, encryption at rest, backups, and much more.
Azure Cosmos DB MongoDB API provides comprehensive support for MongoDB query language constructs. Below you can find the detailed list of currently supported operations, operators, stages, commands and options.
Azure Cosmos DB supports the following database commands on all MongoDB API accounts.
- delete
- find
- findAndModify
- getLastError
- getMore
- insert
- update
- logout
- authenticate
- getnonce
- dropDatabase
- listCollections
- drop
- create
- filemd5
- createIndexes
- listIndexes
- dropIndexes
- connectionStatus
- reIndex
- buildInfo
- collStats
- dbStats
- hostInfo
- listDatabases
- whatsmyuri
Azure Cosmos DB supports aggregation pipeline in public preview. See the Azure blog for instructions on how to onboard to the public preview.
- aggregate
- count
- distinct
- $project
- $match
- $limit
- $skip
- $unwind
- $group
- $sample
- $sort
- $lookup
- $out
- $count
- $and
- $or
- $not
- $setEquals
- $setIntersection
- $setUnion
- $setDifference
- $setIsSubset
- $anyElementTrue
- $allElementsTrue
- $cmp
- $eq
- $gt
- $gte
- $lt
- $lte
- $ne
- $abs
- $add
- $ceil
- $divide
- $exp
- $floor
- $ln
- $log
- $log10
- $mod
- $multiply
- $pow
- $sqrt
- $subtract
- $trunc
- $concat
- $indexOfBytes
- $indexOfCP
- $split
- $strLenBytes
- $strLenCP
- $strcasecmp
- $substr
- $substrBytes
- $substrCP
- $toLower
- $toUpper
- $arrayElemAt
- $concatArrays
- $filter
- $indexOfArray
- $isArray
- $range
- $reverseArray
- $size
- $slice
- $in
- $dayOfYear
- $dayOfMonth
- $dayOfWeek
- $year
- $month
- $week
- $hour
- $minute
- $second
- $millisecond
- $isoDayOfWeek
- $isoWeek
- $cond
- $ifNull
- $sum
- $avg
- $first
- $last
- $max
- $min
- $push
- $addToSet
Following operators are supported with corresponding examples of their use. Consider this sample document used in the queries below:
{
"Volcano Name": "Rainier",
"Country": "United States",
"Region": "US-Washington",
"Location": {
"type": "Point",
"coordinates": [
-121.758,
46.87
]
},
"Elevation": 4392,
"Type": "Stratovolcano",
"Status": "Dendrochronology",
"Last Known Eruption": "Last known eruption from 1800-1899, inclusive"
}
Operator | Example |
---|---|
$eq | { "Volcano Name": { $eq: "Rainier" } } |
$gt | { "Elevation": { $gt: 4000 } } |
$gte | { "Elevation": { $gte: 4392 } } |
$lt | { "Elevation": { $lt: 5000 } } |
$lte | { "Elevation": { $lte: 5000 } } |
$ne | { "Elevation": { $ne: 1 } } |
$in | { "Volcano Name": { $in: ["St. Helens", "Rainier", "Glacier Peak"] } } |
$nin | { "Volcano Name": { $nin: ["Lassen Peak", "Hood", "Baker"] } } |
$or | { $or: [ { Elevation: { $lt: 4000 } }, { "Volcano Name": "Rainier" } ] } |
$and | { $and: [ { Elevation: { $gt: 4000 } }, { "Volcano Name": "Rainier" } ] } |
$not | { "Elevation": { $not: { $gt: 5000 } } } |
$nor | { $nor: [ { "Elevation": { $lt: 4000 } }, { "Volcano Name": "Baker" } ] } |
$exists | { "Status": { $exists: true } } |
$type | { "Status": { $type: "string" } } |
$mod | { "Elevation": { $mod: [ 4, 0 ] } } |
$regex | { "Volcano Name": { $regex: "^Rain"} } |
In $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 whin which field 'x' has value that begins 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/}] })
.
Operator | Example |
---|---|
$geoWithin | { "Location.coordinates": { $geoWithin: { $centerSphere: [ [ -121, 46 ], 5 ] } } } |
$geoIntersects | { "Location.coordinates": { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [ [ [ -121.9, 46.7 ], [ -121.5, 46.7 ], [ -121.5, 46.9 ], [ -121.9, 46.9 ], [ -121.9, 46.7 ] ] ] } } } } |
$near | { "Location.coordinates": { $near: { $geometry: { type: "Polygon", coordinates: [ [ [ -121.9, 46.7 ], [ -121.5, 46.7 ], [ -121.5, 46.9 ], [ -121.9, 46.9 ], [ -121.9, 46.7 ] ] ] } } } } |
$nearSphere | { "Location.coordinates": { $nearSphere : [ -121, 46 ], $maxDistance: 0.50 } } |
$geometry | { "Location.coordinates": { $geoWithin: { $geometry: { type: "Polygon", coordinates: [ [ [ -121.9, 46.7 ], [ -121.5, 46.7 ], [ -121.5, 46.9 ], [ -121.9, 46.9 ], [ -121.9, 46.7 ] ] ] } } } } |
$minDistance | { "Location.coordinates": { $nearSphere : { $geometry: {type: "Point", coordinates: [ -121, 46 ]}, $minDistance: 1000, $maxDistance: 1000000 } } } |
$maxDistance | { "Location.coordinates": { $nearSphere : [ -121, 46 ], $maxDistance: 0.50 } } |
$center | { "Location.coordinates": { $geoWithin: { $center: [ [-121, 46], 1 ] } } } |
$centerSphere | { "Location.coordinates": { $geoWithin: { $centerSphere: [ [ -121, 46 ], 5 ] } } } |
$box | { "Location.coordinates": { $geoWithin: { $box: [ [ 0, 0 ], [ -122, 47 ] ] } } } |
$polygon | { "Location.coordinates": { $near: { $geometry: { type: "Polygon", coordinates: [ [ [ -121.9, 46.7 ], [ -121.5, 46.7 ], [ -121.5, 46.9 ], [ -121.9, 46.9 ], [ -121.9, 46.7 ] ] ] } } } } |
Operator | Example | Notes |
---|---|---|
$all | { "Location.coordinates": { $all: [-121.758, 46.87] } } |
|
$elemMatch | { "Location.coordinates": { $elemMatch: { $lt: 0 } } } |
|
$size | { "Location.coordinates": { $size: 2 } } |
|
$comment | { "Location.coordinates": { $elemMatch: { $lt: 0 } }, $comment: "Negative values"} |
|
$text | Not supported. Use $regex instead |
Following methods are supported:
Method | Example | Notes |
---|---|---|
cursor.sort() | cursor.sort({ "Elevation": -1 }) |
Documents without sort key do not get returned |
Azure Cosmos DB indexes every field in documents that are written to the database by default. 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. Now you can create custom indexes in Azure Cosmos DB by using the createIndex command, including the ‘unique’ constraint.
Unique indexes are available for all MongoDB API accounts.
Azure Cosmos DB supports a relative time-to-live (TTL) based on the timestamp of the document. TTL can be enabled for MongoDB API collections through the Azure portal.
Azure Cosmos DB does not yet support users and roles. Azure Cosmos DB supports role based access control (RBAC) and read-write and read-only passwords/keys that can be obtained through the Azure portal (Connection String page).
Azure Cosmos DB supports automatic, native replication at the lowest layers. This logic is extended out to achieve low-latency, global replication as well. Azure Cosmos DB does not support manual replication commands.
Azure Cosmos DB supports automatic, server-side sharding. Azure Cosmos DB does not support manual sharding commands.
- Learn how to use Studio 3T with an API for MongoDB database.
- Learn how to use Robo 3T with an API for MongoDB database.
- Explore Azure Cosmos DB with protocol support for MongoDB samples.