1

In my NodeJs application I am using MongoDB database. I have two collections products and market_companies. Both collections have a field name hub_id and dimensions, and here dimensions is an object field.

I have created the aggregation pipeline where in both the collections I am comparing hub_id and dimensions field but the thing is hub_id in products collection is an integer but in market_companies it's an string due to which I am not getting desired output.

I want to know how can I convert hub_id to integer from string in market_companies collection.

Below is my code:

     db.products.aggregate([
    {
    $lookup: {
      from: "market_companies",
      let: {
        hubId: "$hubId",
        dimensions: "$dimensions"
      },
      as: "companies",
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$hubId", // How to convert this into Integer
                    "$$hubId"
                  ]
                },
                {
                  $setEquals: [
                    {
                      "$objectToArray": {
                        $ifNull: [
                          "$dimensions",
                          {}
                        ]
                      }
                    },
                    {
                      "$objectToArray": {
                        $ifNull: [
                          "$$dimensions",
                          {}
                        ]
                       }
                     }
                   ]
                 }
               ]
             }
           }
         },
       ]
      }
     }
    ])   
1
  • Hi, please try to edit your question to include more details, such as the language you are using. Commented Dec 31, 2022 at 14:14

1 Answer 1

1

You can do it with $toInt operator:

{
  $eq: [
    { $toInt: "$hubId" },
    "$$hubId"
  ]
},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.