If you want to find the total number of unique users in each collection, you can execute the following commands in the mongo
shell:
db.A.distinct("user").length;
db.B.distinct("user").length;
To determine the number of distinct users in the combination of collections A
and B
, I suggest retrieving distinct arrays for each collection and then merging the arrays to calculate the total count. For JavaScript, utilizing Underscore.js' union()
function is recommended. You can find more information on how to use it here. Remember, you can load Underscore.js (or any JavaScript file) into the mongo
shell by running this command within the shell:
load("path/to/underscore.js");
Subsequently, you can easily perform the following actions:
var a = db.A.distinct("user");
var b = db.B.distinct("user");
_.union(a, b).length;
Alternatively, if you prefer, you can create your own JavaScript function following the guidelines provided here, or develop a solution in your application's respective language.