筛选用户权限子集记录

【问题】

Is there a way to use the $map operator in a regular Mongo document query (or aggregate \$match which I believe is the same thing).

What I’m trying to do is thus: Given an set of sets, return the document if any of the sets is a subset of a parameter.

Example:

Let’s say I have three documents:
{x : [ [“A”,“B”] ] }
{x : [ [“A”, “D”] ] }
{x : [ [“A”,“B”], [“A”,“D”] ] }
and I have an array
auths = [“A”,“B”,“C”]

I want to run a query where I get back the first and third documents because both contain the set [“A”,“B”] which is a subset of auths, but not the second document because its only set contains D which is not in the set of auths

If I were doing this in a $redact pipeline I could do this with something along the lines of:

{“$anyElementTrue” : {
“$map” : {
“input”: “$x”,
“as”: “s”,
“in”: {“$setIsSubset”: [“$$s”, auths] }
}
}}

but when I try to run this as a query I get

BadValue unknown top level operator: $anyElementTrue

【回答】

直接使用 Mongodb 的 API 应该可以实现这个需求,但会比较繁琐,也可以考虑用 SPL 解决:

A

1

=mongo_open(“mongo://localhost:27017/local?user=test&password=test”)

2

=mongo_shell(A1,”test36.find()”)

3

=[“a”,“b”,“c”]

4

=A2.select(.au.pselect(A3.pos())>0)

5

>mongo_close(A1)

A2:

A3:对照序列

A4:对照 A2 每条记录的 au 是不是 A3 的子集,如果是就查出来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值