Henrique Gonçalves
Henrique Gonçalves

Reputation: 1582

Spring MongoDB query regex

I'm trying to query MongoDB using spring but I can't get my regex to work.

I have a tree in mongodb as Materialized Paths as in MongoDB docs (http://www.mongodb.org/display/DOCS/Trees+in+MongoDB).

In the shell the query

db.categories.find({path:/^\w+,$/})

works fine to find the path before the first comma.

Ex.: {"path" : "a,"} gets returned but not {"path: "a,b,"} which is what I want.

How do I make the same query in spring? I've tried:

new Query(Criteria.where("path").regex("/^\\w+,$/"))

but this doesn't work.

Thank you

Upvotes: 3

Views: 8873

Answers (1)

Henrique Gonçalves
Henrique Gonçalves

Reputation: 1582

I just solved my problem.

query = new Query(Criteria.where("path").regex("^([A-Z]|[a-z])+,$"));

although \w stands for "any word character" which usually means alphanumeric, I just need the alphabet characters.

I can later throw [0-9] and \_ to have the same as \w.

If someone can explain how to make \w to work, I'll be very thankful :)

Upvotes: 5

Related Questions