-1

this is my message which is coming for every event. How can i collect only one type data (which is string) and give it a value of 1, and also at the same time insert to rethinkdb as a value 1, and also update it's total value sample data ---- take only "event":"click" as a integer value 1 and insert into database as 1 and also update total value like total_click

{
 "message_ver": "2011-03-17",
 "data":{
   "chan":"chan-552a4",        
   "device":"mob", 
     "event":"click", 
     "type":"ad",   
     "ip":"95.195.139.177",
     "user_agent":"Mozilla/5.0 [FB_IAB/FB4A;FBAV/31.0.0.20.13;]", 
     "content_version":"ver-5534bb69cbeb1",
     "referrer":"http://vlt.com",
    "r_type":"ad"
  }
} 
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42
Istiak Mahmood
  • 2,330
  • 8
  • 31
  • 73
  • This question is not very clear. Can you try explaining what you want to do in a more detailed fashion? Also, is this client-side code? You can't execute RethinkDB queries from the browser. – Jorge Silva Apr 28 '15 at 20:53
  • this is a event type data which will come for every event happen. Now i want o store in rethinkdb but only one or two object which can be "event" and this event i want to store as a integer (value 1 every time) when i sore it into db. Also i want to update the value of "total_event" – Istiak Mahmood Apr 29 '15 at 06:43

1 Answers1

0

It seems that what you want is you want every document/row to represent a different type of event. It seems that what you want is insert a new document into the database if that event type doesn't exist in the database and add a total_event property with a count of 1. If that event type already exists, you could want to increment the total count for that document.

In this scenario, the document will have this structure:

{
  event: 'click'
  total_event: 1
}

Hence, you can use the branch command to see if there is a document with that event name and them insert or update the document depending on the result:

r.expr({ "data":{ "event":"click" }})
  .do(function (_row) {
    return r.branch(
      // Check if there is a row with an `event` property equal to our data.event
      r.table('29929310').filter({ 
        event: _row('data')('event') 
      }).count().gt(0),
      // If there is, add 1 to the `total` property
      r.table('29929310').update(function (_row1) { 
        return { total: _row1('total').add(1) };
      }),
      // If there isn't, insert a new document with a total of `1`
      r.table('29929310').insert({ 
        event: _row('data')('event'),
        total: 1
      })
    )
  })
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42