Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate distinct values in MongoDB

Tags:

mongodb

I have a mongodb db with 18625 collections. It has following keys:

    "_id" : ObjectId("5aab14d2fc08b46adb79d99c"), 
    "game_id" : NumberInt(4), 
    "score_phrase" : "Great", 
    "title" : "NHL 13", 
    "url" : "/games/nhl-13/ps3-128181", 
    "platform" : "PlayStation 3", 
    "score" : 8.5, 
    "genre" : "Sports", 
    "editors_choice" : "N", 
    "release_year" : NumberInt(2012), 
    "release_month" : NumberInt(9), 
    "release_day" : NumberInt(11)

Now, i wish to create another dimension/ collection with only genres.

If i use the following query :

db.ign.aggregate([ {$project: {"genre":1}}, { $out: "dimen_genre" } ]);

It generates 18625 collections, even though there are only 113 distinct genres.

How to apply distinct here and get the collection for genres with only the distinct 113 values. I googled, bt it showed that aggregate and distinct don't work together in mongo. I also tried : db.dimen_genre.distinct('genre').length this showed that in dimension_genre, there are 113 distinct genres.

Precisely, how to make a collection from existing one with only distinct values.

I am really new to NoSQLs.

like image 673
Nidhi Garg Avatar asked Mar 18 '18 17:03

Nidhi Garg


People also ask

How count distinct values in MongoDB?

To count the unique values, use "distinct()" rather than "find()", and "length" rather than "count()". The first argument for "distinct" is the field for which to aggregate distinct values, the second is the conditional statement that specifies which rows to select.

Does MongoDB have aggregation?

In MongoDB, aggregation operations process the data records/documents and return computed results. It collects values from various documents and groups them together and then performs different types of operations on that grouped data like sum, average, minimum, maximum, etc to return a computed result.

What are the differences between using aggregate () and find () in MongoDB?

The Aggregation command is slower than the find command. If you access to the data like ToList() the aggregation command is faster than the find.

What is aggregator in MongoDB?

What is Aggregation in MongoDB? Aggregation is a way of processing a large number of documents in a collection by means of passing them through different stages. The stages make up what is known as a pipeline. The stages in a pipeline can filter, sort, group, reshape and modify documents that pass through the pipeline.


1 Answers

You can use $addToSet to group unique values in one document and then $unwind to get back multiple docs:

db.ign.aggregate([
    {
        $group: {
            _id: null,
            genre: { $addToSet: "$genre" }
        }
    },
    {
        $unwind: "$genre"
    },
    {
        $project: {
            _id: 0
        }
    },
    { $out: "dimen_genre" }
]);
like image 94
mickl Avatar answered Nov 15 '22 08:11

mickl