Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some good ways to store Analytics data in MongoDB?

What are some good ways to store Analytics data using MongoDB for future analysis? I am thinking of doing something like:

> db.analytics.insert( { page: 'product', id: 123, date: new Date('Sept 8, 2010'),
                       pageviews: 222, timeOnPage: 5432 } )

> db.analytics.find()
{ "_id" : ObjectId("4c8e04b1f14d4366465197b8"), "page" : "product", "id" : 123, 
  "date" : "Wed Sep 08 2010 00:00:00 GMT-0700 (PDT)", "pageviews" : 222, 
  "timeOnPage" : 5432 }

which is quite relational. pageviews and timeOnPage can be lumped into

> db.analytics.insert({page: 'product', id: 123, date: new Date('Sept 8, 2010'),
                       data: { pageviews: 222, timeOnPage: 5432 } })

although if using Mongoid (a Rails Object Relation Mapper), then there is more complication for having an additional model.

like image 655
nonopolarity Avatar asked Sep 13 '10 11:09

nonopolarity


People also ask

Is MongoDB good for data analytics?

MongoDB provides the tools and APIs that help them build sophisticated analytics queries. Along with analytics-optimized indexing and storage formats, insights and actions are delivered at low latency with high concurrency.

How do you store data in MongoDB?

Documents are used to store data in MongoDB. These documents are saved in JSON (JavaScript Object Notation) format in MongoDB. JSON documents support embedded fields, allowing related data and data lists to be stored within the document rather than in an external table. JSON is written in the form of name/value pairs.

Which database is best for analytics?

Oracle Database is among the most widely used databases in the industry as they support all data types involving Relational, Graph, Structured, and Unstructured information and is hence considered to be one of the best databases available in the market.

What type of data can be stored in MongoDB?

MongoDB Server stores data using the BSON format which supports some additional data types that are not available using the JSON format. Compared to the legacy mongo shell, MongoDB Shell ( mongosh ) has type handling which is better aligned with the default types used by the MongoDB drivers.


1 Answers

You may want to look at using upserts and the $inc operator on counters. If pageviews increments you can use something like this (selector, operator, upsert set to true)

update( { page: 'product', id: 123, data: { pageviews: 222}) }, 
        { $inc : {  pageviews: 1} }, 
        { upsert : true } )

If no document exists for this record it will create one, if one exists it will increment the pageviews.

It really all depends on how your data is shaped.

This blog post explains more.

like image 190
CountCet Avatar answered Sep 25 '22 15:09

CountCet