Databases Reference
In-Depth Information
Next, you may want to get some statistics of all the ratings for Titanic . To fi nd out the distinct set of
ratings by users (from the possible set of integers between 1 and 5, both inclusive), you could query
as follows:
db.runCommand({ distinct: 'ratings', key: 'rating', query: { movie_id: 1721} });
Ratings for Titanic include all possible cases between 1 and 5 (both inclusive) so the response is like so:
{ “values” : [ 1, 2, 3, 4, 5 ], “ok” : 1 }
runCommand takes the following arguments:
Collection name for the fi eld labeled distinct
Field name for key , whose distinct values would be listed
Query to optionally fi lter the collection
runCommand is slightly different in pattern than the query style you have seen so far because the
collection is fi ltered before the distinct values are searched for. Distinct values for all ratings in
the collection can be listed in a way that you have seen so far, as follows:
db.ratings.distinct(“rating”);
You know from the distinct values that Titanic has all possible ratings from 1 to 5. To see how these
ratings break down by each rating value on the 5-point scale, you could group the counts like so:
db.ratings.group(
... { key: { rating:true },
... initial: { count:0 },
... cond: { movie_id:1721 },
... reduce: function(obj, prev) { prev.count++; }
... }
... );
The output of this grouping query is an array as follows:
[
{
“rating” : 4,
“count” : 500
},
{
“rating” : 1,
“count” : 100
},
{
“rating” : 5,
“count” : 389
},
{
“rating” : 3,
“count” : 381
Search WWH ::




Custom Search