Search code examples
orientdbbaasboxnosql

NoSQL statistics type query with BaasBox - OrientDB 1.7.10


I'm trying out BaasBox v0.9.2 which ships with OrientDB 1.7.10 for a proof of concept web app I want to build. Premise is fairly simple and adding/querying is very straight forward and simple for single collections. What I'm having difficulty trying to visualize is how to get statistics type information in NoSQL.

For example, I have a collection of Contacts (firstName, lastName, address, etc) and a collection of Events (title, date, time, etc) and each Event has an array of attendees.

Objects look something like this:

Contact

{
  "firstName": "John",
  "lastName": "Doe",
  "appId": "64f00bcb-cc04-4ed9-89fc-3b9b1a448dae"
}

Event

{
  "title": "Some Event",
  "eventDate": "2015-02-24 04:46 PM",
  "isoDate": "2015-02-24T20:46:00.000Z",
  "appId": "64f00bcb-cc04-4ed9-89fc-3b9b1a448dae",
  "attendees": [
        {"contactId": "c8ae2767-5fe5-4d41-ad6a-b10bd6e62f03", "attended": true},
        {"contactId": "eacff8ff-b691-4d82-9429-898a097ea43a", "attended": true},
        {"contactId": "70ab166c-c0a0-488a-9d5f-6f0b70a32125", "attended": false},
        {"contactId": "34944c69-ebdb-49c6-bd1b-cdb0d191f124", "attended": true},
        {"contactId": "8907e99e-96d6-46e9-a76f-1f9ce7f760d3", "attended": true}
  ]
}

What I would like to know is:

What was the last event that John Doe attended? How many events has John Doe missed?

I would prefer an example using Baasbox JS-SDK, but the Plugin-API is also acceptable as is re-organizing the data as nothing is set in stone.


Solution

  • you can use any operator/function provided by OrientDB http://www.orientechnologies.com/docs/1.7.8/orientdb.wiki/SQL-Where.html

    In BaasBox you can use the key "fields" for projections and "where" for selections. So in your case the code should be something like:

    BaasBox.loadCollectionWithParams("Event", 
            {
             fields:"max(eventDate) as last_date", //or count(*) as times
             where:"attendees contains (attended=true and contactId='<john_contact_id>')"
            })
            .done(function(res) {
                     console.log("res ", res);
            })
            .fail(function(error) {
                    console.log("error ", error);
            });
    

    The REST API calls are:

    GET /document/event?fields=max(eventDate) as last_date&where=attendees contains (attended=true and contactId='<john_contact_id>')
    
    GET /document/event?fields=count(*) as times&where=attendees contains (attended=true and contactId='<john_contact_id>')
    

    Refs:

    http://www.orientechnologies.com/docs/1.7.8/orientdb.wiki/SQL-Where.html

    http://www.baasbox.com/documentation/?javascript#retrieve-documents (Retrieve documents by a query paragraph)