The JOKER
The JOKER

Reputation: 483

$match in aggregate don't return data in mongodb

I have three tables below is the structure like below Structure of the tables

I'm looking to get a result like below

"type1": [ -- type from Accounts collection
            {
                "_id": "5e97e9a224f62f93d5x3zz46", -- _id from Accounts collection
                "locs": "sampleLocks 1", -- field from Accounts collection
                "solutions": "sample solutions 1", -- field from Accounts collection
                "Clause": "clause 1" -- field from AccountsDesc collection
            },
            {
                "_id": "5e97e9a884f62f93d5x3zz46", 
                "locs": "sampleLocks2",
                "solutions": "sample solutions2", 
                "Clause": "clause2" 
            }
        ],

        "type2": [
         // same data construction as of type1 above
        ]
  1. _id, locks, solution to be coming from Accounts collection
  2. Clause field to be coming from AccountsDesc collection
  3. accounts_id is kind of a foreign key in AccountsDesc coming from Account
  4. competitor_id is kind of a foreign key in AccountsDesc coming from Competitor

Below is what my query looks like

db.accountDesc.aggregate([
  { 
        $match : {accounts_Id : "123456"}, active: true}
  },
  {
    $lookup: {
        from: 'accounts',
        pipeline: [{ $match: { type: { $in: ["type1, type2, type3"] } } }],
        as: 'accountsData'
    }
  },
  {
    $group: {

      _id: "$accountsData.type",
      data: {
           $push:  {_id: "$accountsData._id", clause: "$clause", locs: "$type.locs",  solutions: "$type.solutions"}
       }
    }
  },
   {
    $group: {
        _id: null,
            data: {
                $push: {
                    k: {
                        $toString: '$_id'
                    },
                    v: '$data'
                }
            }
        }
    },
    {
        $replaceRoot: {
            newRoot: {
                $arrayToObject: '$data'
            }
        }
    }
])

Issues related with the query -

  1. $match : {accountId : "123456"}, active: true} -- No data is returned if i use match on AccountsDesc collection
  2. cant set localField, foriegnField if im using pipeline, then how the mapping will happen like a LEFT join.
  3. clause: "$clause" don't get the value of this field in the response

Upvotes: 1

Views: 923

Answers (1)

Valijon
Valijon

Reputation: 13113

As we discussed in chat, you want RIGHT OUTER JOIN for your aggregation.

Try the query below:

db.User_Promo_Map.aggregate([
  {
    $match: {
      user_Id: ObjectId("5e8c1180d59de1704ce68112")
    }
  },
  {
    $lookup: {
      from: "promo",
      pipeline: [
        {
          $match: {
            active: true,
            platform: {
              $in: [
                "twitch",
                "youtube",
                "facebook"
              ]
            }
          }
        }
      ],
      as: "accountsData"
    }
  },
  {
    $unwind: "$accountsData"
  },
  {
    $group: {
      _id: "$accountsData.platform",
      data2: {
        $addToSet: {
          amount: "$amount",
          promo_Id: "$promo_Id"
        }
      },
      data: {
        $addToSet: {
          _id: "$accountsData._id",
          format: "$accountsData.format",
          description: "$accountsData.description"
        }
      }
    }
  },
  {
    $addFields: {
      data: {
        $map: {
          input: "$data",
          as: "data",
          in: {
            "_id": "$$data._id",
            "description": "$$data.description",
            "format": "$$data.format",
            amount: {
              $reduce: {
                input: "$data2",
                initialValue: "$$REMOVE",
                in: {
                  $cond: [
                    {
                      $eq: [
                        "$$this.promo_Id",
                        "$$data._id"
                      ]
                    },
                    "$$this.amount",
                    "$$value"
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  {
    $group: {
      _id: null,
      data: {
        $push: {
          k: {
            $toString: "$_id"
          },
          v: "$data"
        }
      }
    }
  },
  {
    $replaceRoot: {
      newRoot: {
        $arrayToObject: "$data"
      }
    }
  }
])

MongoPlayground

Upvotes: 2

Related Questions