Is there a way to group results from multiple documents when performing aggregation

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am new to mongo and trying to perform aggregation query to calculate min/max of timestamps for a given document.

Sample documents are below -

{
"_id" : ObjectId("5c9cd93adddca9ebb2b3fcba"),
"frequency" : 5,
"s_id" : "30081993",
"timestamp" : NumberLong(1546300800000),
"date" : ISODate("2019-01-01T00:00:00.000Z"),

"values" : {
    "1547439900000" : {
        "number_of_values" : 3,
        "min_value" : 32.13,
        "max_value" : 81.42
    },
    "1547440200000" : {
        "number_of_values" : 3,
        "min_value" : 48.08,
        "max_value" : 84.52
    },
    "1547440500000" : {
        "number_of_values" : 2,
        "min_value" : 27.39,
        "max_value" : 94.64
    }
  }
}

{
"_id" : ObjectId("5c9cd851dddca9ebb2b3f2ac"),
"frequency" : 5,
"s_id" : "27061995",
"timestamp" : NumberLong(1546300800000),
"date" : ISODate("2019-01-01T00:00:00.000Z"),

"values" : {
    "1547539900000" : {
        "number_of_values" : 31,
        "min_value" : 322.13,
        "max_value" : 831.42
    },
    "1547540200000" : {
        "number_of_values" : 3,
        "min_value" : 418.08,
        "max_value" : 8114.52
    },
    "1547740500000" : {
        "number_of_values" : 2,
        "min_value" : 207.39,
        "max_value" : 940.64
    }
  }
}

I have come up with the following query which works for a single document.

    db.testdb.aggregate([
      {
        $match: {
          "s_id": "30081993",
          "frequency": 5,

        }
      },
      {
        $project: {
          _id: 1,
          valuesarray: {
            $objectToArray: "$values"
          }
        }
      },
      {
        $unwind: "$valuesarray"
      },
      {
        $group: {
          "_id": "",
          "min_timestamp": {
            $min: "$valuesarray.k"
          },
          "max_timestamp": {
            $max: "$valuesarray.k"
          }
        }
      }
    
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

 

Yes, only small changes are required to make this work for multiple documents.

In $match stage, specify your $in query:

$match: {
    "s_id": { $in : [ "30081993", "27061995" ] }, 
    "frequency": 5,
}

In $project stage, rename s_id to _id, to ensure we keep the s_id associated with each document:

$project: {
    _id: "$s_id",
    valuesarray: {
        $objectToArray: "$values"
    }
}

In $group stage, group by _id (originally s_id), to ensure we correctly group the timestamps together before calculating $min/$max:

$group: {
    "_id": "$_id",
    "min_timestamp": {
        $min: "$valuesarray.k"
    },
    "max_timestamp": {
        $max: "$valuesarray.k"
    }
}

Whole pipeline:

db.testdb.aggregate([
  {
    $match: {
      "s_id": { $in : [ "30081993", "27061995" ] }, 
      "frequency": 5,
    }
  },
  {
    $project: {
      _id: "$s_id",
      valuesarray: {
        $objectToArray: "$values"
      }
    }
  },
  {
    $unwind: "$valuesarray"
  },
  {
    $group: {
      "_id": "$_id",
      "min_timestamp": {
        $min: "$valuesarray.k"
      },
      "max_timestamp": {
        $max: "$valuesarray.k"
      }
    }
  }
]);

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.