Database Manual / Reference / mongosh Methods / Object Constructors

BSONRegExp() (mongosh method方法)

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()对象

Use 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()对象

Query 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设置为falsemongosh将返回错误:

db.testbson.find( {}, {}, { bsonRegExp: false })
Uncaught:
SyntaxError: Invalid regular expression: /(?-i)AA_/i: Invalid group