Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Wednesday, 15 June 2016

Scaling the Read Query Load On MongoDB server



Requirement Overview:We are capturing all activity on Jobs In mongoDB like view ,apply,view source,apply source etc.There is lots of read query fired on real times to get stats about jobs.
We need to scale our read load so that we can serve all read request without any delay.


Approach :There are 2 ways of reducing read load on Primary MongoDB server.
       
  1. Distributing the Data on Multiple Node (Sharding).
            
     2.  Use secondary mongodb node for read query.


By default, an application directs its read operations to the primary member in a replica set.        




Since Our data size is not large ,we have not implemented sharding .But read operation are very high on the same data Set.
Code Snippet:


var collection =dataLoggerDB.collection(currentCollection,{readPreference:'secondaryPreferred'});


Read Preference Options:


Read Preference Mode
Description
Default mode. All operations read from the current replica set primary.
In most situations, operations read from the primary but if it is unavailable, operations read from secondary members.
All operations read from the secondary members of the replica set.
In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary.
Operations read from member of the replica set with the least network latency, irrespective of the member’s type.

Friday, 29 January 2016

How to find count of multiple fields in MongoDb

Problem Statement:
I have a use case where i need to find the count of activity done on JobId.
I have a mongodb collection jobslog which has following data

jobId,action
1 a (apply)
2       v (view)
3      a (apply)
1 v (view)

I knew the query in mysql
select count(a),count(v) from jobslog group by jobId

But I do not know how to do it in Mongo.So i did lots of search on google for that.

Solution:
Query In MongoDB
db.jobslog.aggregate([
                    { $group: {
                        _id: "$jobId",
                         view_count: { $sum: {
                                        $cond: [ { $eq: [ '$a', 'v' ] }, 1, 0 ]
                                     } },
                         apply_count: { $sum: {
                                        $cond: [ { $eq: [ '$a', 'a' ] }, 1, 0 ]
                                     } },
                     }},
                     { $out : output_collection_name }

])