Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Writing mongo queries for the first time can be quite intimidating - luckily however, the analytics console provides the query playground, for users to build out and test their own custom queries. 

Query Playground

The query playground will let you choose from your project queries to run over the collector you'll select just afterwards. Users will then be prompted to fill in the values required by the query, before being able to execute it. This emulates how a query would be set up within the dashboard, and is extremely helpful for testing and prototyping purposes.

...


Before you begin, it's important to know the shape and structure of the event you're trying to query, especially if this is your first time using this feature. 

Luckily, we

...

can create a query for that:

Code Block
languagejs
themeMidnight
[
	{
		"$sort":{"date":-1}
	},
	{
		"$limit":"#n#"
	}
]

Start by

...

creating a new query called 'Last N events' (or whatever you like, really), by clicking the button.
Next, copy the code above into the Params section. This will be the body of your query, and all your logic should go in here.
This is an aggregate query, which should be

...

selected from the Type drop down

...

menu. This will place a wrapper around the query when it's ran. A find option is available also, but we won't be using that type of query to begin with here. 

This will return all events from the current date, up to the limit specified in "#n#"

Prompting User Input

Note the 'limit' value:

"$limit":"#n#"

The two hash symbols enclose a value that needs to be passed in by the user. Here, it is the value n.

Now your query is created, if you return to the playground and select it, a new blue box titled Search Parameters will appear below the query box. You'll see here it's asking for the value of n

here, we need to pass in the value type, followed by the value. We want to see the last X number of events, so we we need to tell it that it's an integer, and what it's value is.

here, we'll pass in int:2.

This tells the query that the value is an integer, and the integer is 2. 

Before you can run this, you need to select the collector that you wish query. You can read all about how collectors work here.

Hit apply, and watch what happens next. 

Code Block
languagejs
themeMidnight
{
  "utilities": [
    {
      "_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": "594376d9f59a7c6d1cb00e18",
      "name": "Server CLI Request",
      "tags": {
        "value": 1497509263823,
        "label": "aaron",
        "action": "Start",
        "category": "Videos"
      },
      "date": "2017-06-16T06:12:41.402Z",
      "__v": 0
    }
  ]
}

Your output will always be returned in JSON format, inside an utilities array. These events here are custom events collected using the JavaScript SDK. the _id and name values are preconfigured by the SDK, acting as an identifier and the service the request was made from. At this stage, custom analytics events will all be returned with the name "Server CLI Request". The tags section contains the custom analytics data.

...

You can tell the query builder to prompt for input on any value, by enclosing it in the the # symbols.

This is a fairly simple query, and probably won't get you too far in terms of displaying graphs.
Our Query Examples and Tips section will go more in to detail on different query types and how to display the data you wish to see.

Next up, we'll show you how you can test your queries and analyse their output, using the Query Playground. 

Next Step


Query Playground