1

I am attempting to write a query builder for aggrigation's ( Our app has one for the find api, and we have some use cases where aggrigation's are needed )

in find one example we use it for is for pre-filtering document's to only one's they should have access to ( Reduces the load on our auth system ), But I can not seem to find a way to do that with aggregation

How would I re-write this in an aggrigation pipeline?

db.collection.find({
  participants: {
    "$elemMatch": {
      scopes: {
        "$in": [
          "READWRITE",
          "READ",
          "OWNER"
        ],
        "$nin": [
          "EXCLUDE"
        ]
      },
      module_id: {
        "$in": [
          "bdc1ab4d-8c58-48cb-b811-e42a0d778df3",
          "e0f26978-37ea-4415-9213-fb48dbfc3630"
        ]
      }
    }
  }
})

Example Doc's

{
    "title": "One Document",
    "participants": [
      {
        "id": "2464b4a6-96c1-4dca-b764-34e424499e9f",
        "module_name": "USER",
        "module_id": "bdc1ab4d-8c58-48cb-b811-e42a0d778df3",
        "roles": [
          "MEMBER"
        ],
        "scopes": [
          "OWNER"
        ],

      },
      {
        "id": "e0e58850-a6ce-4b89-a527-71bcdd57014a",
        "module_name": "ORGANIZATION",
        "module_id": "e0f26978-37ea-4415-9213-fb48dbfc3630",
        "roles": [
          "MEMBER"
        ],
        "scopes": [
          "READWRITE"
        ],

      }
    ]
  },
  {
    "title": "another document",
    "participants": [
      {
        "id": "9edae792-6fdd-47f4-900d-e6bc11fa8e7a",
        "module_name": "USER",
        "module_id": "579b4b72-5dba-4d0c-bf21-2982e0a2ff94",
        "roles": [
          "MEMBER"
        ],
        "scopes": [
          "OWNER"
        ],

      },
      {
        "id": "b72eea73-837d-492d-aa11-d0edb994b6ee",
        "module_name": "USER",
        "module_id": "bdc1ab4d-8c58-48cb-b811-e42a0d778df3",
        "roles": [
          "MEMBER"
        ],
        "scopes": [
          "READWRITE"
        ],

      },
      {
        "id": "cae84997-079f-4a2b-9b4d-f1b4f152f697",
        "module_name": "ORGANIZATION",
        "module_id": "25b3a235-f6c3-45d6-b64b-bb1e48639bfb",
        "roles": [
          "MEMBER"
        ],
        "scopes": [
          "READWRITE"
        ],

      }
    ]
}
0

1 Answer 1

2

The same can be done in the aggregation pipeline using $match.

db.collection.aggregate([
    {
        $match:{
            "participants":{
                $elemMatch:{
                    "scopes":{
                        $in:[ 'READWRITE', 'READ', 'OWNER' ],
                        $nin:[ 'EXCLUDE' ]
                    },
                    "module_id":{
                        $in:[ 'bdc1ab4d-8c58-48cb-b811-e42a0d778df3','e0f26978-37ea-4415-9213-fb48dbfc3630' ]
                    }
                }
            }
        }
    }
]).pretty()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but how do you return specific sub-document as a result? This will match entire documents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.