0

In java this is possible

public static void main(String[] args) {
    Matcher m = Pattern .compile("^(.*?[.].*?[.].*?[.].*?)[.].*")
                        .matcher(
                                "com.SEM.Google.Generico.space.test");
    if (m.matches()) {
        System.out.println(m.group(1));
    }
}

This would give me as result: com.SEM.Google.Generico

If I have a string in mongodb

"dv" : "com.SEM.Google.Generico.space.test"

can I use the mongo aggregation framework somehow to get com.SEM.Google.Generico as result?

It should be as generic as possible. So not something like

$project: {
    pathString: {
        $substr: ["$path.dv", 0, 23]
    }

}

Is this possible at all? Thanks.

2 Answers 2

2

No, there is no way to do this.

This feature has been requested two years ago, but it hasn't been implemented yet ( see the open jira issue: https://jira.mongodb.org/browse/SERVER-11947 ). If you don't want to use $substr I guess that you should apply the regex on the query results...

Sign up to request clarification or add additional context in comments.

Comments

0
var pattern = "Your Regex Pattern"
db.yourCollectionName.find( { "dv": { $regex: pattern} } )

should produce the desired results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.