处理 mongodb 中匹配正则表达式搜索的字符串

Manipulate strings in mongodb that match the regex search

在java这是可能的

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

这会给我结果:com.SEM.Google.Generico

如果我在 mongodb

中有一个字符串

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

我可以使用 mongo 聚合框架以某种方式获得 com.SEM.Google.Generico 结果吗?

它应该尽可能通用。所以不像

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

}

这可能吗? 谢谢

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

应该会产生预期的结果

不,没有办法做到这一点。

此功能已在两年前提出要求,但尚未实现(请参阅开放的 jira 问题:https://jira.mongodb.org/browse/SERVER-11947)。 如果你不想使用 $substr 我想你应该在查询结果上应用正则表达式...