Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get direct descendants in materialized path in MongoDB using regex

In MongoDB collection, I have materialized path tree model like:

",Books,Programming,Databases,NoSQL,"
",Books,Programming,Databases,SQL,"

For fetching all descendants of Programming used:

db.categories.find( { path: /,Programming,/ } )

Is any availability to create request that will find only direct descendants Programming? I.e. will find records with Databases but not with NoSQL and SQL.

like image 459
Slava Avatar asked Feb 01 '26 12:02

Slava


1 Answers

Use

/,Programming,[^,]+,$/

The [^,]+, pattern matches 1 or more symbols other than a comma ([^,]+) and the , matches a literal comma. $ is the end-of-string anchor.

like image 136
Wiktor Stribiżew Avatar answered Feb 03 '26 06:02

Wiktor Stribiżew