$regexFindAll - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$regexFindAll

在 5.0 中引入

Amazon DocumentDB 中的$regexFindAll运算符用于对文档中的字符串字段执行正则表达式匹配。它允许您搜索和提取与给定正则表达式模式匹配的特定子字符串,返回正则表达式的所有匹配项。

参数

  • input:要搜索的字符串字段或表达式。

  • regex:要匹配的正则表达式模式。

  • options:(可选)为正则表达式指定可选参数的对象,例如区分大小写和多行匹配。支持的选项有i(不区分大小写)和m(多行)。

示例(MongoDB 外壳)

以下示例演示如何使用$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 Query Planner 文档] (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()