Alex T
Alex T

Reputation: 3764

How to return filtered data and unique values in mongodb aggregate

I have the following pipeline:

[
  {
    $project: {
      _id: 0,
      id: 1,
      uniqueId: 1,
      shortLabel: 1,
      "geographicLocation.name": 1,
      "economicConcept.name": 1,
      "frequency.name": 1,
      "scale.name": 1,
      "unit.name": 1,
      source: 1,
    },
  },
  {
    $match: {
      $and: [
        {
          $or: [
            {
              "geographicLocation.name":
                "United States",
            },
            {
              "geographicLocation.name": "Singapore",
            },
          ],
        },
        {
          $or: [
            {
              source: {
                $elemMatch: {
                  name: {
                    $regex: "New",
                    $options: "i",
                  },
                },
              },
            },
          ],
        },
      ],
    },
  },
  {
    $facet: {
      metadata: [
        { $count: "total" },
        { $addFields: { page: 1 } },
      ],
      data: [{ $skip: 0 }, { $limit: 10 }],
    },
  },
]

Which returns filtered dataset.

Now for this I also want to get unique values for few of dataset attributes, so I added this step:

   {
        "$group": {
            "_id": null,
            "distinctRegion": { "$addToSet": "$geographicLocation.name" },
            "distinctFrequency": { "$addToSet": "$frequency.name"},
            "distinctConcept": { "$addToSet": "$economicConcept.name" }
        }
    },

But after adding this, I am not getting the filtered data results, only unique values as output of this pipeline. Is there any way to get both? I want to get filtered dataset and also unique values from that output.

Upvotes: 0

Views: 24

Answers (1)

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

You should add a separate key, within $facet and compute the unique values there. Something like this:

{
    $facet: {
      metadata: [
        { $count: "total" },
        { $addFields: { page: 1 } },
      ],
      data: [{ $skip: 0 }, { $limit: 10 }],
      uniqueValues: [
      {
        "$group": {
            "_id": null,
            "distinctRegion": { "$addToSet": "$geographicLocation.name" },
            "distinctFrequency": { "$addToSet": "$frequency.name"},
            "distinctConcept": { "$addToSet": "$economicConcept.name" }
        }
      }
     ]
    },
}

Upvotes: 1

Related Questions