可能与这个问题有关使用OPA MongoDB高级API的基本GROUP BY语句。
我希望能够检索一个文档列表,其中"名称"字段值包含给定的字符串。
这是我的文件列表:
{name: "Charles-Hugo"},
{name: "Jean Pierre"},
{name: "Pierre Dupont"},
我希望只能检索名称中包含"Pierre"字符串的文件:Jean-Pierre和Pierre Dupont。
我知道使用MongoDB高级API是不可能的。我已经查看了低级API函数,但我不知道在安全的Opa类型中检索文档的最简单方法是什么。
此外,我想将跳过/限制选项添加到我的查询中。
知道吗?
Opa中的DbGen自动化机制支持以下功能:
DbSet.iterator(/path/data[name =~ pattern])
正如@Henri所指出的,自从commit[enhance]DbGen以来,Opa中就支持正则表达式搜索:添加不区分大小写的regex运算符=~这非常好。
请注意,它使用的是$regex
运算符,而不是全文索引,这可能会导致一些性能损失:(正如MongoDB文档所说,$regex运算符以有限的方式使用索引-仅用于前缀搜索:模式^Jean
。在文本中的任何位置搜索Jean
都需要完全扫描。
就我个人而言,我使用Mongo的全文索引功能和Opa的"低级"API来执行$text
命令,如下所示:
function list({float score, Article.id id}) textSearch(string query) {
function onfailure(failure) {
cat.error("textSearch({{~query}}): {failure}");
[];
}
function onsuccess(success) {
function aux(~{name,value}) {
name == "results";
}
match (List.filter(aux, success)) {
| [] :
// `results` field not found - error
onfailure(success);
| results:
cat.debug("textSearch({~{query}}): {results}");
function ({~score, obj: ~{id}}) {
~{score, id}
}
|> List.map(_, Bson.doc2opa(results) ? []);
}
}
opts = [H.str("search", query), H.doc("project", [H.i32("_id",0), H.i32("id",1)])];
// { search: query, project: {_id:0, id:1}, }
// |> Bson.opa2doc
outcome = MongoCommands.simple_str_command_opts(ll_db, db_name, "text", coll_name, opts);
MongoCommon.outcome_map(outcome, onsuccess, onfailure)
}
该功能在Mongo中从2.4开始作为实验性功能(您必须通过特殊的配置选项打开它),在2.6中作为稳定功能(默认打开)。