使用 Azure Cosmos DB for MongoDB 3.2,可以将熟悉的 MongoDB 功能与企业级功能(例如多区域分发、自动分片和高可用性)配合使用。 本文介绍使用 Azure Cosmos DB for MongoDB 3.2 时支持的功能、语法和优势。
重要
Azure Cosmos DB for MongoDB 版本 3.2 没有最新的支持终止计划。 未来支持终止的最低通知是三年。
协议支持
Azure Cosmos DB for MongoDB 的所有新帐户都与 MongoDB 服务器版本 3.6 兼容。 本文介绍 MongoDB 版本 3.2。 此处列出了受支持的运算符以及任何限制或异常。 任何理解这些协议的客户端驱动程序应该都能够连接到 Azure Cosmos DB for MongoDB。
Azure Cosmos DB for MongoDB 还为符合条件的帐户提供无缝升级体验。 有关详细信息,请参阅 MongoDB 版本升级指南。
查询语言支持
Azure Cosmos DB for MongoDB 全面支持 MongoDB 查询语言构造。 可在此处找到当前支持的操作、运算符、阶段、命令和选项的详细列表。
数据库命令
Azure Cosmos DB for MongoDB 支持以下数据库命令:
注意
本文仅列出受支持的服务器命令,并排除客户端包装器函数。 客户端包装器函数(如 deleteMany()
和 updateMany()
)在内部利用 delete()
和 update()
服务器命令。 利用受支持的服务器命令的函数与 Azure Cosmos DB for MongoDB 兼容。
查询和写入操作命令
支持 | |
---|---|
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 |
✅ 是 |
聚合管道
聚合命令
支持 | |
---|---|
aggregate |
✅ 是 |
count |
✅ 是 |
distinct |
✅ 是 |
聚合阶段
支持 | |
---|---|
$project |
✅ 是 |
$match |
✅ 是 |
$limit |
✅ 是 |
$skip |
✅ 是 |
$unwind |
✅ 是 |
$group |
✅ 是 |
$sample |
✅ 是 |
$sort |
✅ 是 |
$lookup |
✅ 是 |
$out |
✅ 是 |
$count |
✅ 是 |
$addFields |
✅ 是 |
聚合表达式
布尔表达式
支持 | |
---|---|
$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 |
✅ 是 |
运算符
以下运算符在其相应的使用示例中受支持。 请考虑以下查询中使用的此示例文档:
{
"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"
}
支持 | 示例 | |
---|---|---|
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"} } |
说明
在 $regex 查询中,左定位表达式允许索引搜索。 但是,使用“i”修饰符(不区分大小写)和“m”修饰符(多行)会导致在所有表达式中进行回收集合扫描。
如果需要包含 $
或 |
,最好创建两个(或更多)正则表达式查询。 例如,给定以下原始查询:find({x:{$regex: /^abc$/})
,必须按如下所示进行修改:
find({x:{$regex: /^abc/, x:{$regex:/^abc$/}})
第一部分使用索引将搜索限制为从开头 ^abc
的文档,第二部分与确切条目匹配。 条形运算符 |
充当“或”函数 - 查询 find({x:{$regex: /^abc |^def/})
与字段 x
具有以 "abc"
或开头 "def"
的值的文档匹配。 若要利用索引,将查询分解为由 $or 运算符联接的两个不同的查询: find( {$or : [{x: $regex: /^abc/}, {$regex: /^def/}] })
更新运算符
字段更新运算符
支持 | |
---|---|
$inc |
✅ 是 |
$mul |
✅ 是 |
$rename |
✅ 是 |
$setOnInsert |
✅ 是 |
$set |
✅ 是 |
$unset |
✅ 是 |
$min |
✅ 是 |
$max |
✅ 是 |
$currentDate |
✅ 是 |
数组更新运算符
支持 | |
---|---|
$addToSet |
✅ 是 |
$pop |
✅ 是 |
$pullAll |
✅ 是 |
$pull |
✅ 是 |
$pushAll |
✅ 是 |
$push |
✅ 是 |
$each |
✅ 是 |
$slice |
✅ 是 |
$sort |
✅ 是 |
$position |
✅ 是 |
小窍门
$pull
不支持条件为 。
位更新运算符
支持 | |
---|---|
$bit |
✅ 是 |
地理空间运算符
支持 | 示例 | |
---|---|---|
$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 ] ] ] } } } } |
排序操作
使用 findOneAndUpdate
操作时,支持基于单个字段的排序操作,但不支持基于多个字段的排序操作。
其他运算符
支持 | 示例 | 说明 | |
---|---|---|---|
$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 |
✅ 是 | 不支持。 改为使用 $regex。 |
不支持的运算符
Azure Cosmos DB for MongoDB 不支持 $where
和 $eval
运算符。
方法
支持下列方法:
游标方法
支持 | 示例 | 说明 | |
---|---|---|---|
cursor.sort() |
✅ 是 | cursor.sort({ "Elevation": -1 }) |
不会返回没有排序键的文档 |
唯一索引
默认情况下,Azure Cosmos DB 为写入数据库的文档中的每个字段编制索引。 唯一索引确保特定字段在一个集合的所有文档中都不会有重复值,类似于默认 _id
键保持唯一性的方式。 可以在 Azure Cosmos DB 中使用 createIndex 命令(包括“unique”约束)创建自定义索引。
所有使用 Azure Cosmos DB for MongoDB 的 Azure Cosmos DB 帐户都能使用唯一索引。
生存时间 (TTL)
Azure Cosmos DB 仅在版本 3.2 中支持集合级别 (_ts) 的生存时间 (TTL)。 升级到版本 3.6+ 以利用其他形式的 TTL。
用户和角色管理
Azure Cosmos DB 尚不支持用户和角色。 但是,Azure Cosmos DB 支持通过 Azure 门户(连接字符串页)获取的 Azure 基于角色的访问控制,以及读写和只读的密码/密钥。
复制
Azure Cosmos DB 支持在最低层进行自动本机复制。 此逻辑还可以扩展,实现低延迟的多区域复制。 Azure Cosmos DB 不支持手动复制命令。
写关注
某些应用程序依赖写关注,后者指定写入操作期间需要的响应数。 考虑到 Azure Cosmos DB 在后台处理复制的方式,所有写入在默认情况下都自动成为仲裁。 由客户端代码指定的任何写关注都会被系统忽略。 有关详细信息,请参阅使用一致性级别最大化可用性和性能。
分片
Azure Cosmos DB 支持服务器端自动分片。 它自动管理分片的创建、放置和均衡。 Azure Cosmos DB 不支持手动分片命令,这意味着你不必调用命令,例如 addShard
, balancerStart
和 moveChunk
。 只需在创建容器或查询数据时指定分片键。