Azure Cosmos DB for MongoDB (3.2 版):支援的功能和語法

適用於: MongoDB

Azure Cosmos DB 為 Microsoft 的全域散發多重模型資料庫服務。 您可以使用任何開放原始碼 MongoDB 用戶端驅動程式與 Azure Cosmos DB for MongoDB 進行通訊。 Azure Cosmos DB for MongoDB 依照 MongoDB 有線通訊協定,支援使用現有的用戶端驅動程式。

使用 Azure Cosmos DB for MongoDB 可讓您享有既有的 MongoDB 權益,以及 Azure Cosmos DB 提供的所有企業功能:全域散發自動分區化、可用性和延遲保證、每個欄位的自動編製索引、待用加密、備份等等。

注意

Azure Cosmos DB for MongoDB 3.2 版目前沒有生命週期結束 (EOL) 計劃。 未來的 EOL 至少會在三年前通知。

通訊協定支援

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

Set expressions

  • $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' 修飾詞 (多行) 會在所有運算式中造成集合掃描。 需要包含 '$' 或 '|' 時,最好先建立兩個 (或更多) regex 查詢。 例如,假設原始查詢如下:find({x:{$regex: /^abc$/}),則必須修改為:find({x:{$regex: /^abc/, x:{$regex:/^abc$/}})。 第一個部分會使用索引來僅限搜尋以 ^abc 開頭的文件,而第二個部分會比對完整項目。 垂直線運算子 '|' 的作用如同 "or" 函式 - 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 (注意:不支援有條件的 $pull)
  • $pushAll
  • $push
  • $each
  • $slice
  • $sort
  • $position

位元更新運算子

  • $bit

地理空間運算子

運算子 範例 支援
$geoWithin { "Location.coordinates": { $geoWithin: { $centerSphere: [ [ -121, 46 ], 5 ] } } } Yes
$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 ] ] ] } } } } Yes

排序作業

使用 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 不支援 $where$eval 運算子。

方法

支援下列方法:

指標方法

方法 範例 備註
cursor.sort() cursor.sort({ "Elevation": -1 }) 沒有排序索引鍵的文件不會傳回

唯一索引

Azure Cosmos DB 會為文件中預設寫入資料庫的每個欄位編制索引。 唯一索引可確保集合中所有文件的特定欄位沒有重複值,類似於預設 _id 索引鍵上會保留唯一性的方式。 您可以使用 createIndex 命令 (包括 'unique' 限制式) 在 Azure Cosmos DB 中建立自訂索引。

所有使用 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 RBAC) 及讀寫和唯讀密碼/金鑰。

複寫

Azure Cosmos DB 支援最低層級的自動、原生複寫。 此邏輯也可延伸至達到低延遲且全域的複寫。 Azure Cosmos DB 不支援手動複寫命令。

寫入考量

有些應用程式依賴寫入考量,其會指定寫入作業期間所需的回應數目。 由於 Azure Cosmos DB 在背景中處理複寫的方式,所有的寫入依預設都會自動仲裁。 用戶端程式碼所指定的任何寫入考量都會受到忽略。 深入了解使用一致性層級將可用性和效能最大化

分區化

Azure Cosmos DB 支援自動與伺服器端的分區化。 它會自動管理分區的建立、放置和平衡。 Azure Cosmos DB 不支援手動將命令分區化,這表示您不需要叫用 shardCollection、addShard、balancerStart、moveChunk 之類的命令。您只需要在建立容器或查詢資料時指定分區索引鍵。

下一步

  • 了解如何使用 Studio 3T 搭配 Azure Cosmos DB for MongoDB。
  • 了解如何使用 Robo 3T 搭配 Azure Cosmos DB for MongoDB。
  • 使用 Azure Cosmos DB for MongoDB 探索 MongoDB 範例