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

適用於: 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.6 版目前沒有生命週期結束 (EOL) 計劃。 未來的 EOL 至少會在三年前通知。

通訊協定支援

針對新帳戶,Azure Cosmos DB for MongoDB 依預設會與 MongoDB 伺服器 3.6 版相容。 以下列出支援的運算子及任何限制或例外狀況。 任何了解這些通訊協定的用戶端驅動程式,都應能夠連線至 Azure Cosmos DB for MongoDB。 當您建立適用於 MongoDB 的 Azure Cosmos DB API 帳戶時,3.6 版的帳戶會具有 *.mongo.cosmos.azure.com 格式的端點,而 3.2 版的帳戶則具有 *.documents.azure.com 格式的端點。

查詢語言支援

Azure Cosmos DB for MongoDB 為 MongoDB 查詢語言建構提供全面支援。 下列各節顯示 Azure Cosmos DB 目前支援的伺服器作業、運算子、階段、命令和選項詳細清單。

注意

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

資料庫命令

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

查詢和寫入作業命令

Command 支援
change streams Yes
delete .是
eval
find .是
findAndModify .是
getLastError .是
getMore .是
getPrevError
insert .是
parallelCollectionScan
resetError
update 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

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 .是
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

資料類型

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 開頭的文件,而第二個部分會比對完整項目。 垂直線運算子 find({x:{$regex: /^abc |^def/}) 的作用如同 "or" 函式 - 查詢 | 會比對欄位 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 支援各種不同的索引,可讓您依多個欄位排序、改善查詢效能及強制執行唯一性。

GridFS

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

複寫

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

可重試的寫入

Cosmos DB 尚不支援可重試的寫入。 用戶端驅動程式必須將 retryWrites=false 加入其連接字串中。

分區化

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

工作階段

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

存留時間 (TTL)

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

使用者和角色管理

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

寫入考量

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

下一步