I have database XXX and collection YYY in MongoDB (using MongoLab).
Here is a sample record:
{
"_id": {
"$oid": "551a5asdfsdfsdfs"
},
"_class": "com.test.com",
"mvid": "d0fffsdfs"
}
I would like to retrieve all distinct values of the field "mvid" and count them. Here is what found on the internet:
db.YYY.aggregate([{ $group: { _id: "$mvid"} },{ $group: { _id: 1, count: { $sum: 1 } } }])
I also tried
XXX.YYY.aggregate([{ $group: { _id: "$mvid"} },{ $group: { _id: 1, count: { $sum: 1 } } }])
I put the above into command text box, in both cases I get the following error message: "We encountered an error while parsing your JSON. Please check your syntax (e.g. ensure you are using double quotes around both your field names and values) and try again."
What am I doing wrong?
That command text box is for providing JSON-formatted parameters to whatever command is selected in the drop-down. aggregate is not one of the listed commands, so it appears that you'd need to install the MongoDB shell locally and then remotely connect to your MongoLab database and run the command.
See this blog post for details on installing the MongoDB shell and connecting to MongoLab.
For finding the counts by each mvid:
db.YYY.aggregate([{$group: {_id: "$mvid", count: {$sum: 1}}}])
For finding the count of distinct mvids:
db.YYY.aggregate([{$group: {_id: "$mvid", count: {$sum: 1}}}, {$group: {_id: null, count: {$sum: 1}}}, {$project: {_id: 0, count: 1}}])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With