Analytics Console: Creating Queries
- Aaron Roworth
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:
[ { "$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.
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.