Database Manual / Reference / mongosh Methods / Object Constructors

BSONRegExp() (mongosh method)

Definition

Creates a new BSON type for a regular expression.

Syntax

BSONRegExp has the following syntax:

BSONRegExp("<pattern>, "<flags>")
ParameterTypeDescription

pattern

string

The regular expression pattern. You must not wrap the pattern with delimiter characters.

flag

string

The regular expression flags. Characters in this argument are sorted alphabetically.

Examples

Insert a BSONRegExp() Object

Use the BSONRegExp() constructor to create the BSON regular expression.

var bsonRegExp = BSONRegExp("(?-i)AA_", "i")

Insert the object into the testbson collection.

db.testbson.insertOne( { foo: bsonRegExp } )

Retrieve a BSONRegExp() Object

Query the testbson collection for the inserted document.

db.testbson.find( {}, {}, { bsonRegExp: true } )

You can see the binary BSON regular expressions stored in the collection.

[
{
_id: ObjectId('65e8ba8a4b3c33a76e6cacca'),
foo: BSONRegExp('(?-i)AA_', 'i')
}
]

If you set bsonRegExp to false, mongosh returns an error:

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