Count Distinct Values via aggregation framework

Q: Is it possible to count distinct values of a field in mongodb?

A: Yes! This can be done via the aggregation framework in mongo. This takes two group commands; the first groups by all the distinct values, and the second does a count of them all.

pipeline = [ 
    { $group: { _id: "$myNonUniqueFieldId"}  },
    { $group: { _id: 1, count: { $sum: 1 } } }
];

db.runCommand( 
    {
    "aggregate": "collection" , 
    "pipeline": pipeline
    }
);
Advertisements

Passed my M202: MongoDB Advanced Deployment and Operations Course!

The course is a 7 week online programme and then final exam. The course is well recommended – https://university.mongodb.com/courses/10gen/M202/2014_September/about

image

image

error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:128

When attempting to read data on a secondary, the following error is recieved:

error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:128

This happens because you are connected to a secondary and slaveOK is set to false. When connecting via  a shell, use the following to enable reads:

rs.slaveOk()

The one caveat with this is that on a system with many secondary’s, there can sometimes be latency between the master and secondary’s which may result in stale data. slaveOk works on the proviso that the user is happy reading data based on eventual consistency.

When connecting via an application, reading from a secondary is achieved via “Read preference”

error: { "$err" : "not master or secondary; cannot currently read from this replSet member", "code" : 13436 }

When attempting to connect to a mongo standalone server without replica set from an application server, the following error is recieved:

error: { "$err" : "not master or secondary; cannot currently read from this replSet member", "code" : 13436 }

On investigation it was found that /etc/mongo.conf had the follwoing parameter set:

replSet        = repqamongo

which means that the mongod process was started as an uninitialised replica.

To resolve, either remove the flag and restart mongod process or run rs.initiate() to turn into a single node replica (a bit pointless though)