Compass Export Pipeline to bson.M{} instead of bson.D{}

It’s great that you’ve added the export to Go language, but I think we should have the option to export in bson.M{} instead of the bson.D{} it is right now.
The reason is that it’s a lot easier on big pipelines to simply export it on a file, and then copy and paste that directly to Compass. While the bson.D requires extensive manipulations of that export before injecting it into Compass.
Here is an example of an export from Compass:

bson.A{
    bson.D{
        {"$lookup",
            bson.D{
                {"as", "lwData"},
                {"from", "invUnits"},
                {"let", bson.D{{"pid", "$_id"}}},
                {"pipeline",
                    bson.A{
                        bson.D{
                            {"$match",
                                bson.D{
                                    {"$expr",
                                        bson.D{
                                            {"$eq",
                                                bson.A{
                                                    "$productId",
                                                    "$$pid",
                                                },
                                            },
                                        },
                                    },
                                    {"productType", "assort"},
                                    {"transfers.soldInfo", bson.D{{"$exists", false}}},
                                },
                            },
                        },
                        bson.D{{"$group", bson.D{{"_id", 1}}}},
                    },
                },
            },
        },
    },
}

The output of that is:

[
  [
    {
      "Key": "$lookup",
      "Value": [
        {
          "Key": "as",
          "Value": "lwData"
        },
        {
          "Key": "from",
          "Value": "invUnits"
        },
        {
          "Key": "let",
          "Value": [
            {
              "Key": "pid",
              "Value": "$_id"
            }
          ]
        },
        {
          "Key": "pipeline",
          "Value": [
            [
              {
                "Key": "$match",
                "Value": [
                  {
                    "Key": "$expr",
                    "Value": [
                      {
                        "Key": "$eq",
                        "Value": [
                          "$productId",
                          "$$pid"
                        ]
                      }
                    ]
                  },
                  {
                    "Key": "productType",
                    "Value": "assort"
                  },
                  {
                    "Key": "transfers.soldInfo",
                    "Value": [
                      {
                        "Key": "$exists",
                        "Value": false
                      }
                    ]
                  }
                ]
              }
            ],
            [
              {
                "Key": "$group",
                "Value": [
                  {
                    "Key": "_id",
                    "Value": 1
                  }
                ]
              }
            ]
          ]
        }
      ]
    }
  ]
]

With all the Key and Value, we need to go through each of them and modify, or have to rewrite the full thing, which can cause some problem if you do any mistakes, not even talking about the time to do it on a 10+ pipeline!

Now the same thing with bson.M

bson.A{
		bson.M{"$lookup": bson.M{
			"from": "invUnits",
			"let":  bson.M{"pid": "$_id"},
			"as":   "lwData",
			"pipeline": bson.A{
				bson.M{"$match": bson.M{
					"productType":        product.Type.Assortment,
					"$expr":              bson.M{"$eq": bson.A{"$productId", "$$pid"}},
					"transfers.soldInfo": bson.M{"$exists": false}},
				},
				bson.M{"$group": bson.M{"_id": 1}},
			},
		}},
	}

And the output:

[
  {
    "$lookup": {
      "as": "lwData",
      "from": "invUnits",
      "let": {
        "pid": "$_id"
      },
      "pipeline": [
        {
          "$match": {
            "$expr": {
              "$eq": [
                "$productId",
                "$$pid"
              ]
            },
            "productType": "assort",
            "transfers.soldInfo": {
              "$exists": false
            }
          }
        },
        {
          "$group": {
            "_id": 1
          }
        }
      ]
    }
  }
]

As you can see, it’s a lot more condensed, also you can just use that output directly!

I’m aware that in some situation it may fail, for example

{$match: {
  $or: [ { type: "product"}, {type: "virtual"} ],
  $or: [ { sku: "1234"}, {sku: "5678"} ],
}}

But it seems that this is already not allowed, so I think it’s safe.

1 Like

Is it something that you can consider doing? It would save me quite some time to have it, because for now I can’t use the Go export as it takes 5x more time to format the data than it is with a normal JSON.

For people curious how to do it, you can use ChatGPT, it converts very well.