Aggregate pipeline returning inconsistent results every time when we hit mongo db

Problem: My aggregate pipeline is giving an inconsistent result intermittently when called multiple times through a go code.

Below is the pipeline that we are executing through mongodb Go Driver. This is resulting in wrong results occassionally.

[
  {
    $match:
      /**
       * query: The query in MQL.
       */
      {
        "subjects.type": "accessgroup_id",
        type: "access",
        $or: [
          {
            resources: {
              $elemMatch: {
                attributes: {
                  $elemMatch: {
                    name: "serviceName",
                    operator: "equals",
                    values: {
                      $elemMatch: {
                        $in: [
                          "mcmp:core-lite:service",
                        ],
                      },
                    },
                  },
                },
              },
            },
          },
        ],
      },
  },
  {
    $unwind:
      /**
       * path: Path to the array field.
       * includeArrayIndex: Optional name for index.
       * preserveNullAndEmptyArrays: Optional
       *   toggle to unwind null and empty values.
       */
      {
        path: "$subjects",
        preserveNullAndEmptyArrays: true,
      },
  },
  {
    $lookup:
      /**
       * from: The target collection.
       * localField: The local join field.
       * foreignField: The target join field.
       * as: The name for the results.
       * pipeline: Optional pipeline to run on the foreign collection.
       * let: Optional variables to use in the pipeline field stages.
       */
      {
        from: "accessgroup",
        let: {
          group_id: {
            $toObjectId: "$subjects.value",
          },
        },
        pipeline: [
          {
            $match: {
              $expr: {
                $eq: ["$_id", "$$group_id"],
              },
            },
          },
        ],
        as: "groups",
      },
  },
  {
    $unwind:
      /**
       * path: Path to the array field.
       * includeArrayIndex: Optional name for index.
       * preserveNullAndEmptyArrays: Optional
       *   toggle to unwind null and empty values.
       */
      {
        path: "$groups",
        preserveNullAndEmptyArrays: true,
      },
  },
  {
    $match:
      /**
       * query: The query in MQL.
       */
      {
        "groups.members": {
          type: "iam_id",
          value: "2012",
        },
      },
  },
]

This is basically working on two collections in a db.

  1. policy → Aggregate query running on this collection
  2. accessgroup

→ Document in “policy” collection

/** 
* Paste one or more documents here
*/
{
  "created_at": {
    "$date": "2023-07-07T05:56:00.319Z"
  },
  "description": "",
  "etag": "20-36db8fc8b7d07df08a1b38404bd9654ce40b430f",
  "resources": [
    {
      "attributes": [
        {
          "name": "serviceName",
          "values": [
            "mcmp:core-lite:service"
          ],
          "operator": "equals"
        }
      ],
      "accesstags": []
    }
  ],
  "roles": [
    {
      "type": "platform",
      "role_id": "krn:v1:mcmp:public:core-lite:iam:::role:administrator"
    }
  ],
  "subjects": [
    {
      "type": "accessgroup_id",
      "value": "64a6ec1ffcbb0b7bfb7111ff"
    }
  ],
  "type": "access",
  "updated_at": {
    "$date": "2023-07-07T05:56:00.319Z"
  }
}

→ Document in “accessgroup” collection

{
  "_id": {
    "$oid": "64a6ec1ffcbb0b7bfb7111ff"
  },
  "created_at": {
    "$date": "2023-07-07T07:39:00.239Z"
  },
  "description": "",
  "etag": "20-4d9756036d550c3e0cc0d931df80cefbbefda793",
  "isFederated": false,
  "members": [
    {
      "type": "iam_id",
      "value": "2011"
    },
    {
      "type": "iam_id",
      "value": "2012"
    },
    {
      "type": "iam_id",
      "value": "2013"
    }
  ],
  "name": "testapikey",
  "rules": [],
  "tenantId": "64a6e9fffcbb0b7bfb7111d4",
  "updated_at": {
    "$date": "2023-07-07T07:39:00.239Z"
  }
}

Is there any known issue with go-mongodriver with aggregation module?

When you say “Wrong Results” or inconsistent, is it just a completely different output or different order or sometimes more or less documents?
Can you repro this strangeness via Compass or the shell if you execute it multiple times?

Thanks John.
Always I should be getting N documents, but intermittently I get less than N.

That is strange indeed! I don’t use the go driver so unaware of any issues, the only think I can think to suggest is to try running the query repeatedly from the shell and see if you can repro it to try and narrow if down if you’ve not already done that.
Hopefully someone else has some more useful ideas!