Analytics Console: Query Examples and Tips


Mongo Documentation

We use find and aggregrate mongo query types. You can read up on how the mongo aggregation pipeline works here.

Taking our event from earlier, let's write some queries to display some of the data we were seeing.


Events
{
  "utilities": [
    {
      "_id": "594376d9f59a7c6d1cb00e18",
      "name": "Server CLI Request",
      "tags": {
        "value": 1497509263823,
        "label": "aaron",
        "action": "Start",
        "category": "Videos"
      },
      "date": "2017-06-16T06:12:41.402Z",
      "__v": 0
    },
    {
      "_id": "594376d9f59a7c6d1cb00e19",
      "name": "Server CLI Request",
      "tags": {
        "value": 1497509350209,
        "label": "kynan",
        "action": "Stop",
        "category": "Videos"
      },
      "date": "2017-06-16T06:12:41.402Z",
      "__v": 0
    },
    {
      "_id": "59437648f59a7c6d1cb00e12",
      "name": "Server CLI Request",
      "tags": {
        "value": 1497509263823,
        "label": "aaron",
        "action": "Start",
        "category": "Videos"
      },
      "date": "2017-06-16T06:10:15.991Z",
      "__v": 0
    },
    {
      "_id": "59437648f59a7c6d1cb00e13",
      "name": "Server CLI Request",
      "tags": {
        "value": 1497509350209,
        "label": "kynan",
        "action": "Stop",
        "category": "Videos"
      },
      "date": "2017-06-16T06:10:15.991Z",
      "__v": 0
    },
    {
      "_id": "59437348f59a7c6d1cb00e01",
      "name": "Server CLI Request",
      "tags": {
        "value": 1497592616045,
        "label": "HowTo",
        "action": "Start",
        "category": "Videos"
      },
      "date": "2017-06-16T05:57:28.370Z",
      "__v": 0
    },
    {
      "_id": "594372b4f59a7c6d1cb00dfd",
      "name": "Server CLI Request",
      "tags": {
        "value": 1497592500120,
        "label": "HowTo",
        "action": "Start",
        "category": "Videos"
      },
      "date": "2017-06-16T05:55:00.120Z",
      "__v": 0
    }
  ]
}


Requests By Tag Name in Last X Weeks

Query
[  
   {  
      "$match":{  
         "tags.category":"#category#",
         "tags.action": "#action#",
    "date":{  
            "$gte":"#gte#",
            "$lte":"#lte#"
         }
      }
   },
   {  
      "$project":{  
         "action":"$tags.action"
      }
   },
   {  
      "$group":{  
         "_id":"$action",
         "count":{  
            "$sum":1
         }
      }
   }
]


Query Parameters

Click the image to increase the size.

Result
{
  "utilities": [
    {
      "_id": "Start",
      "count": 4
    }
  ]
}


Actions in Specific Category

Query
[
  {
    "$match": {
      "tags.request.answerSpace.name": "#answerspace#",
      "tags.request.answerSpace.form": {
        "$exists": true
      },
      "date": {
        "$gte": "#gte#",
        "$lte": "#lte#"
      }
    }
  },
  {
    "$project": {
      "form": "$tags.request.answerSpace.form"
    }
  },
  {
    "$group": {
      "_id": "$form",
      "count": {
        "$sum": 1
      }
    }
  }
]
 


Click the image to increase the size.

Result
{
  "utilities": [
    {
      "_id": "Stop",
      "count": 2
    },
    {
      "_id": "Start",
      "count": 4
    }
  ]
}