$regexFindAll - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

$regexFindAll

在 5.0 中推出

Amazon DocumentDB 中的$regexFindAll運算子用於對文件中的字串欄位執行規則運算式比對。它可讓您搜尋和擷取符合指定規則表達式模式的特定子字串,並傳回規則表達式的所有相符項目。

參數

  • input:要搜尋的字串欄位或表達式。

  • regex:要比對的規則表達式模式。

  • options:(選用) 指定規則表達式選用參數的物件,例如區分大小寫和多行比對。支援的選項為 i(不區分大小寫) 和 m(多行)。

範例 (MongoDB Shell)

下列範例示範如何使用 $regexFindAll運算子從 email 欄位擷取所有字母序列。

建立範例文件

db.users.insertMany([ { _id: 1, name: "John Doe", email: "john@example.com", phone: "555-1234" }, { _id: 2, name: "Jane Roe", email: "jane@example.com", phone: "555-5678" }, { _id: 3, name: "Carlos Salazar", email: "carlos@example.com", phone: "555-3456" }, { _id: 4, name: "Saanvi Sarkar", email: "saanvi@example.com", phone: "555-7890" } ]);

查詢範例

db.users.aggregate([ { $project: { name: 1, emailMatches: { $regexFindAll: { input: '$email', regex: '[a-z]+', options: 'i' } } } } ])

輸出

[ { _id: 1, name: 'John Doe', emailMatches: [ { match: 'john', idx: 0, captures: [] }, { match: 'example', idx: 5, captures: [] }, { match: 'com', idx: 13, captures: [] } ] }, { _id: 2, name: 'Jane Roe', emailMatches: [ { match: 'jane', idx: 0, captures: [] }, { match: 'example', idx: 5, captures: [] }, { match: 'com', idx: 13, captures: [] } ] }, { _id: 3, name: 'Carlos Salazar', emailMatches: [ { match: 'carlos', idx: 0, captures: [] }, { match: 'example', idx: 7, captures: [] }, { match: 'com', idx: 15, captures: [] } ] }, { _id: 4, name: 'Saanvi Sarkar', emailMatches: [ { match: 'saanvi', idx: 0, captures: [] }, { match: 'example', idx: 7, captures: [] }, { match: 'com', idx: 15, captures: [] } ] } ]

注意:如果您的查詢使用 Amazon DocumentDB 規劃器第 1 版,您必須使用提示來使用索引。如果沒有提示,查詢可能會執行集合掃描。若要檢查您的規劃器版本並進一步了解如何使用提示,請參閱 【Amazon DocumentDB 查詢規劃器文件】(https://docs.aws.amazon.com/documentdb/latest/developerguide/query-planner.html://)。

程式碼範例

若要檢視使用 $regexFindAll命令的程式碼範例,請選擇您要使用的語言標籤:

Node.js

以下是在 Node.js 應用程式中使用 $regexFind 運算子的範例:

const { MongoClient } = require('mongodb'); async function main() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const users = db.collection('users'); const results = await users.aggregate([ { $project: { name: 1, emailMatches: { $regexFindAll: { input: "$email", regex: "[a-z]+", options: "i" } } } } ]).toArray(); console.log(JSON.stringify(results, null, 2)); await client.close(); } main();
Python

以下是在 Python 應用程式中使用 $regexFind 運算子的範例:

from pymongo import MongoClient client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] users = db['users'] results = list(users.aggregate([ { "$project": { "name": 1, "emailMatches": { "$regexFindAll": { "input": "$email", "regex": "[a-z]+", "options": "i" } } } } ])) print(results) client.close()