Docs Menu
Docs Home
/
MongoDB Manual
/ / /

BSONRegExp()

On this page

  • Definition
  • Syntax
  • Examples

Creates a new BSON type for a regular expression.

BSONRegExp has the following syntax:

BSONRegExp("<pattern>, "<flags>")
Parameter
Type
Description
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.

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 } )

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

Back

BinData