Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose Aggregate with $unwind before $group

I'm having a lot of difficult to get solve this mongodb (mongoose) problem.

There is schema Guess (_id , title , tags) with tags is array of tags_id ( was referenced from Tag schema ).

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var schema = mongoose.Schema({
    title: {
        type: Schema.Types.Mixed,
        required: true
    },
    tags: [{
        type: Schema.Types.ObjectId,
        ref: 'Tag',
        required: false,
        default: []
    }]
});
schema.index({ '$**': 'text' });

module.exports = mongoose.model('Guess', schema);

document example :

{
    "_id" : ObjectId("578857529507cafd5fe1f9b3"),
    "title" : {
        "en" : "Will Amazon start its first drone deliveries this year?"
    },
    "tags" : [ 
        ObjectId("578857529507cafd5fe1f9b7"), 
        ObjectId("578857529507cafd5fe1f9b6")
    ]
}
{
    "_id" : ObjectId("578857a59507cafd5fe1f9bb"),
    "title" : {
        "en" : "Will Singapore 3D print homes during 2016?"
    },
    "tags" : [ 
        ObjectId("578857a59507cafd5fe1f9bf"), 
        ObjectId("578857529507cafd5fe1f9b6")
    ]
}
......
.....

I need to get list of most popular tag (tags). Above I show you the schema and my solution mongoose query.

    Guess.aggregate([
                {
                    "$project": {
                        "_id": 1,
                        "tags": 1
                    }
                },
                {
                    "$unwind": "$tags"
                }
                // ,
                // {
                //    "$group": {
                //        "tags": '$tags.tags',
                //        "count": {"$sum": 1}
                //    }
                // },
                // {"$sort": {"count": -1}},
                // {"$limit": 5}
            ], function (error, result) {
                // 
            });

if I comment this option

        {
          "$group": {
              "tags": '$tags.tags',
              "count": {"$sum": 1}
           }
        },
        {"$sort": {"count": -1}},
        {"$limit": 5}

Then my result of query is

[ { _id: 578857529507cafd5fe1f9b3,
    tags: 578857529507cafd5fe1f9b7 },
  { _id: 578857529507cafd5fe1f9b3,
    tags: 578857529507cafd5fe1f9b6 },
  { _id: 578857a59507cafd5fe1f9bb,
    tags: 578857a59507cafd5fe1f9bf },
  { _id: 578857a59507cafd5fe1f9bb,
    tags: 578857a59507cafd5fe1f9be },
  { _id: 578857d637cc983f60774fcb,
    tags: 578857d637cc983f60774fcf },
  { _id: 578857d637cc983f60774fcb,
    tags: 578857d637cc983f60774fce },
  { _id: 578f2cbe875ec80f11e49a66,
    tags: 578f2cbe875ec80f11e49a6a },
  { _id: 578f2e25b470bb62115f1741,
    tags: 578f2e25b470bb62115f1745 },
  { _id: 578f2f119cd9848c1180be8b,
    tags: 578f2f119cd9848c1180be8f },
  { _id: 578f2f119cd9848c1180be8b,
    tags: 578f2f119cd9848c1180be8e },
  { _id: 578f50876a3ba88d13aed982,
    tags: 578f50876a3ba88d13aed986 },
  { _id: 578f50876a3ba88d13aed982,
    tags: 578f50876a3ba88d13aed985 },
  { _id: 578f510c6a3ba88d13aed989,
    tags: 578f510c6a3ba88d13aed98c },
  { _id: 578f510c6a3ba88d13aed989,
    tags: 578f510c6a3ba88d13aed98d } ]

I uncomment then this query error. I want group tag and display 5 most popular tags. How to fix it?

The result that I want looks like

[ { tags: 578857529507cafd5fe1f9b6, count: 10 },
  { tags: 578857529507cafd5fe1f9b7, count: 9 },
  { tags: 578854779507cafd5fe1f9a6, count: 8 },
  { tags: 578854779507cafd5fe1f9a5, count: 5 },
  { tags: 5788570d9507cafd5fe1f9b0, count: 2 } ]
like image 242
Loint Avatar asked Aug 26 '16 04:08

Loint


People also ask

What is the purpose of the $group operator in an aggregate pipeline?

Definition. The $group stage separates documents into groups according to a "group key". The output is one document for each unique group key. A group key is often a field, or group of fields.

What is $unwind in mongoose?

$unwind treats the sizes field as a single element array if: the field is present, the value is not null, and. the value is not an empty array.

What is the use of $unwind in MongoDB?

The MongoDB $unwind operator is used to deconstruct an array field in a document and create separate output documents for each item in the array.

Is aggregation slow in MongoDB?

Aggregation is slow - Working with Data - MongoDB Developer Community Forums.


1 Answers

In your $group stage you need to specify an _id. It's mandatory and will be the group by key. So basically you need to change:

{
   "$group": {
       "tags": '$tags.tags',
       "count": {"$sum": 1}
   }
}

to:

{
   "$group": {
       "_id": '$tags',
       "count": {"$sum": 1}
   }
}

So the complete pipeline would be:

Guess.aggregate([
    {
       "$project": {
           "_id": 1,
           "tags": 1
       }
    },
    {
       "$unwind": "$tags"
    }
    ,
    {
       "$group": {
           "_id": '$tags',
           "count": {"$sum": 1}
       }
    },
    {"$sort": {"count": -1}},
    {"$limit": 5}
]); 
like image 130
Wake Avatar answered Sep 19 '22 14:09

Wake