Definition定义
Creates a new BSON type for a regular expression.为正则表达式创建新的BSON类型。
Syntax语法
BSONRegExp has the following syntax:具有以下语法:
BSONRegExp("<pattern>, "<flags>")-
Parameter参数Type类型Description描述patternstring字符串The regular expression pattern. You must not wrap the pattern with delimiter characters.正则表达式模式。您不能用分隔符包裹模式。flagstring字符串The regular expression flags. Characters in this argument are sorted alphabetically.正则表达式标志。此参数中的字符按字母顺序排序。
Examples示例
Insert a BSONRegExp() Object插入BSONRegExp()对象
BSONRegExp() ObjectUse the 使用BSONRegExp() constructor to create the BSON regular expression.BSONRegEx()构造函数创建BSON正则表达式。
var bsonRegExp = BSONRegExp("(?-i)AA_", "i")
Insert the object into the 将对象插入testbson collection.testbson集合。
db.testbson.insertOne( { foo: bsonRegExp } )Retrieve a BSONRegExp() Object检索BSONRegExp()对象
BSONRegExp() ObjectQuery the 查询插入文档的testbson collection for the inserted document.testbson集合。
db.testbson.find( {}, {}, { bsonRegExp: true } )
You can see the binary BSON regular expressions stored in the collection.您可以看到存储在集合中的二进制BSON正则表达式。
[
{
_id: ObjectId('65e8ba8a4b3c33a76e6cacca'),
foo: BSONRegExp('(?-i)AA_', 'i')
}
]
If you set 如果将bsonRegExp to false, mongosh returns an error:bsonRegExp设置为false,mongosh将返回错误:
db.testbson.find( {}, {}, { bsonRegExp: false })
Uncaught:
SyntaxError: Invalid regular expression: /(?-i)AA_/i: Invalid group