Azure Cosmos DB for MongoDB (4.0 伺服器版本):支援的功能和語法

適用於: 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。 當您建立 Azure Cosmos DB for MongoDB 帳戶時,3.6+ 版的帳戶會具有 *.mongo.cosmos.azure.com 格式的端點,而 3.2 版的帳戶則具有 *.documents.azure.com 格式的端點。

注意

本文只會列出支援的伺服器命令,並排除用戶端包裝函式。 deleteMany()updateMany() 等用戶端包裝函式會在內部利用 delete()update() 伺服器命令。 使用支援伺服器命令的函式與 Azure Cosmos DB for MongoDB 相容。

查詢語言支援

Azure Cosmos DB for MongoDB 為 MongoDB 查詢語言建構提供全面支援。 以下詳細列出目前支援的作業、運算子、階段、命令和選項。

資料庫命令

Azure Cosmos DB for MongoDB 支援下列資料庫命令:

查詢和寫入作業命令

Command 支援
change streams Yes
delete .是
eval
find .是
findAndModify .是
getLastError .是
getMore .是
getPrevError
insert .是
parallelCollectionScan
resetError
update Yes

交易命令

Command 支援
abortTransaction Yes
commitTransaction Yes

驗證命令

Command 支援
authenticate Yes
getnonce .是
logout Yes

系統管理命令

Command 支援
cloneCollectionAsCapped No
collMod
connectionStatus
convertToCapped
copydb
create .是
createIndexes .是
currentOp .是
drop .是
dropDatabase .是
dropIndexes .是
filemd5 .是
killCursors .是
killOp
listCollections .是
listDatabases .是
listIndexes .是
reIndex .是
renameCollection No

診斷命令

Command 支援
buildInfo Yes
collStats .是
connPoolStats
connectionStatus
dataSize
dbHash
dbStats .是
explain .是
features
hostInfo .是
listDatabases .是
listCommands
profiler
serverStatus
top
whatsmyuri Yes

彙總管線

彙總命令

Command 支援
aggregate Yes
count .是
distinct .是
mapReduce No

彙總階段

Command 支援
addFields
bucket
bucketAuto
changeStream .是
collStats
count .是
currentOp
facet .是
geoNear .是
graphLookup .是
group .是
indexStats
limit .是
listLocalSessions
listSessions No
lookup Partial
match Yes
out .是
project .是
redact .是
replaceRoot .是
replaceWith
sample .是
skip .是
sort .是
sortByCount .是
unwind

注意

$lookup 尚不支援伺服器 3.6 版所引進無關的子查詢功能。 如果您嘗試搭配 letpipeline 欄位使用 $lookup 運算子,您將會收到錯誤訊息,指出 let is not supported

布林運算式

Command 支援
and Yes
not .是
or Yes

轉換運算式

Command 支援
convert Yes
toBool .是
toDate .是
toDecimal .是
toDouble .是
toInt .是
toLong .是
toObjectId .是
toString Yes

Set expressions

Command 支援
setEquals Yes
setIntersection .是
setUnion .是
setDifference .是
setIsSubset .是
anyElementTrue .是
allElementsTrue Yes

比較運算式

注意

適用於 MongoDB 的 API 不支援在查詢中使用含有陣列常值的比較運算式。

Command 支援
cmp Yes
eq .是
gt .是
gte .是
lt .是
lte .是
ne .是
in .是
nin Yes

算術運算式

Command 支援
abs Yes
add .是
ceil .是
divide .是
exp .是
floor .是
ln .是
log .是
log10 .是
mod .是
multiply .是
pow .是
sqrt .是
subtract .是
trunc Yes

字串運算式

Command 支援
concat Yes
indexOfBytes .是
indexOfCP .是
ltrim .是
rtrim .是
trim .是
split .是
strLenBytes .是
strLenCP .是
strcasecmp .是
substr .是
substrBytes .是
substrCP .是
toLower .是
toUpper Yes

文字搜尋運算子

Command 支援
meta No

陣列運算式

Command 支援
arrayElemAt Yes
arrayToObject .是
concatArrays .是
filter .是
indexOfArray .是
isArray .是
objectToArray .是
range .是
reverseArray .是
reduce .是
size .是
slice .是
zip .是
in Yes

變數運算子

Command 支援
map Yes
let Yes

系統變數

Command 支援
$$CURRENT Yes
$$DESCEND .是
$$KEEP .是
$$PRUNE .是
$$REMOVE .是
$$ROOT Yes

常值運算子

Command 支援
literal Yes

日期運算式

Command 支援
dayOfYear Yes
dayOfMonth .是
dayOfWeek .是
year .是
month .是
week .是
hour .是
minute .是
second .是
millisecond .是
dateToString .是
isoDayOfWeek .是
isoWeek .是
dateFromParts .是
dateToParts .是
dateFromString .是
isoWeekYear Yes

條件運算式

Command 支援
cond Yes
ifNull .是
switch Yes

資料類型運算子

Command 支援
type Yes

累加器運算式

Command 支援
sum Yes
avg .是
first .是
last .是
max .是
min .是
push .是
addToSet .是
stdDevPop .是
stdDevSamp Yes

合併運算子

Command 支援
mergeObjects Yes

資料類型

Azure Cosmos DB for MongoDB 支援以 MongoDB BSON 格式編碼的文件。 4.0 API 版本增強了此格式的內部使用,以改善效能並降低成本。 透過執行 4.0+ 的端點撰寫或更新的文件,可經由最佳化獲益。

升級案例中,在升級至 4.0+ 版之前撰寫的文件,必須透過 4.0+ 端點的寫入作業來更新,才能受益於增強的效能。

16 MB 的文件支援會將文件的大小限制從 2 MB 提高到 16 MB。 這項限制僅適用於啟用此功能之後所建立的集合。 為資料庫帳戶啟用此功能之後,就無法停用此功能。

您可以藉由新增 "EnableMongo16MBDocumentSupport" 功能,在 Azure 入口網站的功能索引標籤中啟用 16 MB。

建議啟用伺服器端重試,並避免使用萬用字元索引,以確保大型文件中要求成功。 視需要提高 DB/集合 RU,也可能提高效能。

Command 支援
Double Yes
String .是
Object .是
Array .是
Binary Data .是
ObjectId .是
Boolean .是
Date .是
Null .是
32-bit Integer (int) .是
Timestamp .是
64-bit Integer (long) .是
MinKey .是
MaxKey .是
Decimal128 .是
Regular Expression .是
JavaScript .是
JavaScript (with scope) .是
Undefined Yes

索引和索引屬性

索引數

Command 支援
Single Field Index Yes
Compound Index .是
Multikey Index .是
Text Index
2dsphere .是
2d Index
Hashed Index No

索引屬性

Command 支援
TTL Yes
Unique .是
Partial
Case Insensitive
Sparse
Background Yes

操作員

邏輯運算子

Command 支援
or Yes
and .是
not .是
nor Yes

元素運算子

Command 支援
exists Yes
type Yes

評估查詢運算子

Command 支援
expr
jsonSchema
mod .是
regex Yes
text 否 (不支援。請改用 $regex。)
where No

在 $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/}] })

陣列運算子

Command 支援
all Yes
elemMatch .是
size Yes

註解運算子

Command 支援
comment Yes

投射運算子

Command 支援
elemMatch
meta
slice Yes

更新運算子

欄位更新運算子

Command 支援
inc Yes
mul .是
rename .是
setOnInsert .是
set .是
unset .是
min .是
max .是
currentDate Yes

陣列更新運算子

Command 支援
$ Yes
$[] .是
$[\<identifier\>] .是
addToSet .是
pop .是
pullAll .是
pull .是
push .是
pushAll Yes

更新修飾詞

Command 支援
each Yes
slice .是
sort .是
position Yes

位元更新運算子

Command 支援
bit
bitsAllSet
bitsAnySet
bitsAllClear
bitsAnyClear No

地理空間運算子

運算子 支援
$geoWithin Yes
$geoIntersects .是
$near .是
$nearSphere .是
$geometry .是
$minDistance .是
$maxDistance .是
$center
$centerSphere
$box
$polygon No

排序作業

搭配使用 findOneAndUpdate 作業與 API for MongoDB 4.0 版時,支援對單一欄位和多個欄位進行排序作業。 多個欄位的排序作業是過去有線通訊協定的限制。

編製索引

API for MongoDB 支援各種不同的索引,可讓您依多個欄位排序、改善查詢效能及強制執行唯一性。

GridFS

Azure Cosmos DB 透過任何與 GridFS 相容的 Mongo 驅動程式來支援 GridFS。

複寫

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

可重試的寫入

可重試的寫入可讓 MongoDB 驅動程式在失敗時自動重試特定的寫入作業,但對於特定作業會有更嚴格的需求,這符合 MongoDB 通訊協定需求。 啟用此功能後,分區化集合中的更新作業 (包括刪除) 需要將分區索引鍵納入查詢篩選或更新陳述式中。

例如,假設有一個分區化集合,依索引鍵 "country" 分區化:若要刪除欄位 city = "NYC" 的所有文件,則在已啟用「可重試寫入」時,應用程式必須針對所有分區索引鍵 (country) 值執行作業。

  • 「成功」
  • db.coll.deleteMany({"city": "NYC"}) - 失敗並出現錯誤 ShardKeyNotFound(61)

注意

可重試的寫入目前不支援大量未排序的寫入。 如果您想要執行已啟用可重試寫入的大量寫入,請執行大量排序寫入。

若要啟用此功能,請在資料庫帳戶中新增 EnableMongoRetryableWrites 功能。 此功能也可在 Azure 入口網站的功能索引標籤中啟用。

分區化

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

工作階段

Azure Cosmos DB 尚不支援伺服器端的工作階段命令。

存留時間 (TTL)

Azure Cosmos DB 支援以文件時間戳記為基礎的存留時間 (TTL)。 您可從 Azure 入口網站為集合啟用 TTL。

交易

不分區集合支援多文件交易。 跨集合或分區集合不支援多文件交易。 交易逾時固定為 5 秒。

使用者和角色管理

Azure Cosmos DB 尚不支援使用者和角色。 不過,Azure Cosmos DB 支援可透過 Azure 入口網站 ([連接字串] 頁面) 取得的 Azure 角色型存取控制 (Azure RBAC) 及讀寫和唯讀密碼/金鑰。

寫入考量

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

下一步