Tim Holum
Tim Holum

Reputation: 737

$elemMatch in an MongoDB aggregation

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"
        ],

      }
    ]
}

Upvotes: 1

Views: 635

Answers (1)

Himanshu Sharma
Himanshu Sharma

Reputation: 3010

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()

Upvotes: 2

Related Questions