Mongodb aggregation auto update code to nodejs

I’m looking to retrieve the source code from an aggregation pipeline in MongoDB Compass using Node.js. Could you provide guidance on how to do this?

Hey @D_ng_D,

Welcome to the MongoDB Community forums :sparkles:

Could you please confirm if you are referring to exporting Pipeline to Specific Programming Language. If so, I’ve attached the documentation link above which outlines the steps to export the aggregation pipeline to Node.js.

If my understanding is incorrect or if you have further questions, please feel free to reply to this thread.

Best regards,
Kushagra

1 Like

Thank you for your assistance. However, I would like Node.js to automatically fetch the code from the pipeline and execute it without shutting down the server.

Hey @D_ng_D,

Could you please help us understand with an example or a code snippet, so the community can better assist you?

Best regards,
Kushagra

  vitri: 1,
  input: {
    chan34: "$chan34",
    chan39: "$chan39",
    chan32: "$chan32",
    chan33: "$chan33",
    chan35: "$chan35",
    chan36: "$chan36",
  },
  output: {
    $switch: {
      branches: [
        {
          case: {
            $lt: ["$chan34", 200],
          },
          then: 1,
        },
        {
          case: {
            $lt: ["$chan39", 200],
          },
          then: 2,
        },
        {
          case: {
            $lt: ["$chan32", 200],
          },
          then: 3,
        },
        {
          case: {
            $lt: ["$chan33", 200],
          },
          then: 4,
        },
        {
          case: {
            $lt: ["$chan35", 200],
          },
          then: 20,
        },
        {
          case: {
            $lt: ["$chan36", 200],
          },
          then: 6,
        },
      ],
      default: 0,
    },
  },
}

I have a code for aggregations that runs successfully when executed once after clicking the ‘run’ button on the client. However, when I transfer this code to Node.js to run it repeatedly, I face the challenge of needing to stop the server each time I want to make changes to the aggregation details. I am looking for a solution that allows my server to automatically update the aggregation code by retrieving it from the aggregation pipeline rather than the field of collection.