简体   繁体   English

Restheart Aggregation Framework - 可选的Avars?

[英]Restheart Aggregation Framework - Optional Avars?

We have an aggregation query in RestHeart which works well. 我们在RestHeart中有一个聚合查询,效果很好。

In the match section, we allow users to specify a specific document Id, to return just that document: match部分中,我们允许用户指定特定的文档ID,以便仅返回该文档:

                "_$match" : {
                    "_id" : {
                        "_$var" : "n"
                    },

However, we've also like users to be able to omit this, and get all documents in the collection. 但是,我们也希望用户能够省略这一点,并获取集合中的所有文档。

Is there any way to make avars optional, or to specify a wildcard for that avar ? 有没有办法让avars可选,或者为那个avar指定一个通配符? Currently if the user omits the avar, they get an error. 目前,如果用户省略了avar,则会出错。 And setting the avar to null or an empty string literally matches those values. 并将avar设置为null或空字符串字面上匹配这些值。

I guess another alternative would be if there can be conditional logic in the aggregation query properties -- not sure if this is possible. 我想另一种选择是聚合查询属性中是否存在条件逻辑 - 不确定这是否可行。

Thanks 谢谢

Tom 汤姆

I have found a simpler solution. 我找到了一个更简单的解决方案。 The aggregation query in the _properties collection can be kept as-is, but instead, a wildcard can be specified in the GET request. _properties集合中的聚合查询可以保持原样,但可以在GET请求中指定通配符。 Here is an example: 这是一个例子:

GET https://test.restheartapi.com:4443/DataBase/collection/_aggrs/items?avars={"n":/.*/, "m":/.*/ }

... So although it isn't possible to make avars optional in RestHeart, it's still possible to specify a wildcard as the value. ...因此,尽管在RestHeart中无法使avars成为可选项,但仍可以指定通配符作为值。

I'm not sure if there's a small performance hit by using a regular expression like this, but it seems to be the only simple way to do this for now. 我不确定使用像这样的正则表达式是否有小的性能损失,但它似乎是目前唯一的简单方法。

You can have a conditional aggregation using the $cond operator 您可以使用$ cond运算符进行条件聚合

Evaluates a boolean expression to return one of the two specified return expressions. 计算布尔表达式以返回两个指定的返回表达式之一。

The $cond expression has one of two syntaxes: $ cond表达式具有以下两种语法之一:

{ $cond: { if: <boolean-expression>, then: <true-case>, else: <false-case-> } }

Or: 要么:

{ $cond: [ <boolean-expression>, <true-case>, <false-case> ] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM