本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$regexMatch
5.0 版的新功能。Elastic 叢集不支援。
Amazon DocumentDB 中的$regexMatch運算子用於對字串欄位執行規則運算式比對。它會傳回布林值 (true 或 false),指出輸入字串是否符合指定的模式。
參數
-
input:針對規則表達式進行測試的字串。 -
regex:要比對的規則表達式模式。 -
options:(選用) 修改規則表達式行為的旗標,例如不區分大小寫的比對 (i) 或多行比對 ()m。
範例 (MongoDB Shell)
下列範例示範如何使用 $regexMatch 運算子來檢查名稱開頭是否為字母 'M'。運算子false會為每個文件傳回 true或 。
建立範例文件
db.users.insertMany([ { "_id":1, name: "María García", email: "maría@example.com" }, { "_id":2, name: "Arnav Desai", email: "arnav@example.com" }, { "_id":3, name: "Martha Rivera", email: "martha@example.com" }, { "_id":4, name: "Richard Roe", email: "richard@example.com" }, ]);
查詢範例
db.users.aggregate([ { $project: { name: 1, startsWithM: { $regexMatch: { input: "$name", regex: "^M", options: "i" } } } } ]);
輸出
{ _id: 1, name: 'María García', startsWithM: true },
{ _id: 2, name: 'Arnav Desai', startsWithM: false },
{ _id: 3, name: 'Martha Rivera', startsWithM: true },
{ _id: 4, name: 'Richard Roe', startsWithM: false }
程式碼範例
若要檢視使用 $regexMatch命令的程式碼範例,請選擇您要使用的語言標籤: