Aquarius_Girl
Aquarius_Girl

Reputation: 22936

Syntax for regular expressions in MongoDB

> db.inventory.find( )
{ "_id" : ObjectId("5eb67598bee5213484d45087"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5eb67598bee5213484d45088"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5eb67598bee5213484d45089"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
{ "_id" : ObjectId("5eb67598bee5213484d4508a"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5eb67598bee5213484d4508b"), "item" : "postcard", "qty" : 45, "status" : "A", "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "tags" : [ "blue" ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be04608"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be04609"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460a"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460b"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460c"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] }
> 

I intend to find item data where alphabet z is not used:

> db.inventory.find( { item: { $regex: /^z/, $options: 'm'} } )
> db.inventory.find( { item: { $regex: /^z/ } } )
>

These did not provide any results.

This page https://regexr.com/ shows that [^abc] can be used for not a, b, or c.
Please point out the mistake.

Upvotes: 0

Views: 667

Answers (2)

Joe
Joe

Reputation: 28356

MongoDB uses Perl Compatible Regular Expressions

Inside a character class, the caret indicates negation: /[^abc]/ matches a string containing any characters except a, b, or c. This would match "test", "tackle", and "cat", but not "cab".

At the start of a pattern, the caret matches the beginning of a string: /^z/ matches only if the compared string begins with z.

The dollar sign can be used to match the end of a string: /z$/ matches only if the string ends with z.

The asterisk can be used to match repetitions of a character or class, /[^z]*/ will match zero or more characters that are not z

If you want to match only if the string does not contain a z, you can either use a negated character class with repetition anchored to both the beginning and end of the string:

db.inventory.find( { item: { $regex: /^[^z]*$/ } } )

or you can use an unanchored match for z with the $not operator:

db.inventory.find( { item: { $not: /z/ } } )

see pcre2pattern specification for more detail.

Upvotes: 1

Hossam
Hossam

Reputation: 363

use $not operator to perform logical not operation

db.inventory.find( { item: { $not: /^z/ } } )

Upvotes: 1

Related Questions