In the mongo shell I can find unique field values thus:
> db.email_events.distinct('sg_message_id', {'campaign_id':
'22'}).length
2629
>
In C++ I can do it thus, but with a transfer of all unique values from
the server:
bsoncxx::string::view_or_value field_name = "sg_message_id";
bsoncxx::builder::basic::document doc;
doc.append(kvp("campaign_id", "22"));
mongocxx::cursor cursor = email_events.distinct(field_name, doc.view());
int num = 0;
for (auto doc : cursor) {
// This loop will execute 2629 times (today).
cout << bsoncxx::to_json(doc) << endl;
++num;
}
cout << num << endl;
Is there a way to query just the number as in the shell example? Once I
have the cursor it's clearly too late. The collection::distinct()
function does not want a count option, using the count function it's
unclear how to specify a field name for which I want distinct values.
I've not found help in the examples, browsing the code, via google...
Thanks.
--
Jeff Abrahamson
+33 6 24 40 01 57
+44 7920 594 255
http://p27.eu/jeff/