Reputation: 2378
I'm trying to find some information about the users in my Meteor mongo db.
I have a roles package installed and I want to find the number of admin users, business users, consumers etc.
"roles" is like the user "emails" attribute in the sense that it is an array.
So for example if I want to find the count of admin users in the roles array, how would I do it. I'd image it would be something like this:
Meteor.users.find({roles: ['admin']}).count()
Thank you
Upvotes: 1
Views: 394
Reputation: 151072
I presume the format of your documents is actually something like this:
{ "name": "Bill", "roles": [ "admin", "user" ] }
{ "name": "Ted", "roles": [ "admin", "user", "moderator" ] }
{ "name": "Death", "roles": [ "user", "moderator" ] }
In which case a query that specifies looking for "admin" like this:
Meteor.users.find({ "roles": ["admin"] }).count()
Would not match any documents at all, since this form would require "admin" to the be only element in the array. That is what your query basically asks for in the form of an "exact match" to the content in "roles".
Meteor still applies the basic rules of MongoDB even where all operators are not presently supported in minimongo for the browser client. But your query is actually quite simple presuming that that data is unique for each role on each user. Just issue like this:
Meteor.users.find({ "roles": "admin" }).count()
So as long as one of the elements in the roles array actually matches "admin" then that document is counted within the results.
It's a common misconception that an "array" has to be treated with special operators for queries. It is in fact the other way around where operators such as $in
and $nin
as well as $all
and others all themselves take an "array" of arguments to test against an element. With the exception of $all
, most operators don't need to be applied to an array as such and can be applied to "any" field. And none of them make sense unless you need to test for the presence of more than one value at a time.
If you just need admin, then query as shown. If you need either "user" or "admin" use $in
:
Meteor.users.find({ "roles": { "$in": ["user", "admin"] } }).count()
Same for "Bill" or "Ted" in "name", which is not an array:
Meteor.users.find({ "name": { "$in": ["Bill", "Ted"] } }).count()
Or if you need both "admin" and "moderator" in the array use $all
Meteor.users.find({ "roles": { "$all": ["moderator", "admin"] } }).count()
But you don't need these operators generally unless you are testing for more than one value. So just test for the value
Upvotes: 4
Reputation: 541
you can to execute this, remember execute it in the server because minimongo doesn't support all querys...
Meteor.users.find({ roles: { $all: [ "admin"] } }).count();
The $all operator selects the documents where the value of a field is an array that contains all the specified elements. To specify an $all expression, use the following prototype:
http://docs.mongodb.org/manual/reference/operator/query/all/#op._S_all
Upvotes: 3