Data Aggregation

OBTO platform allows the execution of aggregation operations provided by MongoDB. “Aggregation operations process data records and return computed results”. To read more on the different types of available operations, please visit MongoDB’s website on Aggregations.

Following examples illustrates how an aggregation structure will look like. You will first need to create a Data Source under Analytics tab. It takes in the following parameters:

Name: unique name for this aggregation ( must be single word)

Label: Friendly label (spaces allowed)

Collection: collection name on which this aggregation is to run

Script: the aggregation code. It has the following object structure:

  • iproject: contains all fields that are required for aggregation

  • match: query to match records by.

  • group: defines how to group records.

  • project: defines what fields to project in the final result.

  • sort: what field to sort final data on.

The script below is an example which fetches the attendance of all students in a class and groups them by the total counts for each student in class. Note: all operations used in the code below can be found on MongoDB website.

Code

{
  "iproject": {
    "present": {
      "$cond": {
        "if": {
          "$eq": ["$present", true]
        },
        "then": 1,
        "else": 0
      }
    },
    "domain": 1,
    "class_name": 1,
    "student_name": 1,
  },
  "match": {
    "class_name": "{{className}}"
  },
  "group": {
    "_id": {
      "name": "$student_name._id"
    },
    "present": {
      "$sum": "$present"
    },
    "total": {
      "$sum": 1
    }
  },
  "project": {
    "present": "$present",
    "total": "$total",
    "name": "$_id.name",
    "_id": 0
  },
  "sort": {
    "name": 1
  }
}

Last updated