Get User Privilege Mongo Documents That Contain a Set Which Is the Subset of a Parameter
【Question】
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 this: given a 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
【Answer】
I believe MongoDB API is able to do this though it takes a roundabout route. A more direct way is available if you need. That is Structured Process Language (SPL). The code is as below:
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: Define a sequence parameter according to which the user privilege sets are matched.
A4: Match each of A2’s records to see if au contains a set which is the subset of A3’s set, and get it if the result is true.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL