How to Match a UUID inside Expr() in a Lookup using the MongoDB Java Driver

I am using the MongoDB Java Driver to do a lookup between two collections using this method: Bson lookup(final String from, @Nullable final List<Variable> let, final List<? extends Bson> pipeline, final String as)

I have a match stage which uses expr() because I need to use a variable introduced by let. But I also have to match on a UUID.

This gives me the desired results in Compass itself but I am not sure how to translate it to Java. When I hit the convert to Java button in Compass, it says “Symbol ‘UUID’ is undefined”

{
  "from": "person",
  "let": {
    "id": "$id"
  },
  "pipeline": [
    {
      "$match": {
        "$expr": {
          "$and": [
            {
              "$eq": [
                "$artifactId",
                "$$id"
              ]
            },
            {
              "$eq": [
                "$personId",
                UUID("d52aa8ae-bb1a-4e54-8450-bd633bfb6213")
              ]
            }
          ]
        }
      }
    }
  ],
  "as": "matchedField"
}

I have tried writing the UUID out like this but it tells me “An object representing an expression must have exactly one field”. This is how we usually write our UUIDs out in queries.

{
   "$binary": "1piYCuzrS7aXLEazXydQFw==",
   "$type": "04"
}

I also tried pulling the UUID into its own match stage which does not use expr since it’s not using a let variable. This didn’t work because it thinks $binary is a field in one of the collections.

Hello @Raine_Jordan, welcome to the MongoDB forum.

Please post samples of the two collections used in the aggregation. Also, specify the versions of Compass, Java Driver and the MongoDB server.