node.js - 在 $group 之前使用 $unwind 进行 Mongoose 聚合

标签 node.js mongodb

我很难解决这个 mongodb (mongoose) 问题。

Guess (_id , title , tags) 与 tags 是 tags_id 数组(从 Tag 架构中引用)。

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);

文档示例:

{
    "_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")
    ]
}
......
.....

我需要获取最受欢迎的标签(标签)列表。上面我向您展示了架构和我的解决方案 mongoose 查询。

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

如果我评论这个选项

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

那么我的查询结果是

[ { _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 } ]

然后我取消注释此查询错误。我想要分组 tag 并显示 5 个最受欢迎的标签。如何解决?

我想要的结果是这样的

[ { tags: 578857529507cafd5fe1f9b6, count: 10 },
  { tags: 578857529507cafd5fe1f9b7, count: 9 },
  { tags: 578854779507cafd5fe1f9a6, count: 8 },
  { tags: 578854779507cafd5fe1f9a5, count: 5 },
  { tags: 5788570d9507cafd5fe1f9b0, count: 2 } ]

最佳答案

在您的 $group 阶段,您需要指定一个 _id。它是强制性的,将是按键组。所以基本上你需要改变:

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

到:

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

所以完整的管道将是:

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

关于node.js - 在 $group 之前使用 $unwind 进行 Mongoose 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158286/

相关文章:

javascript - 在 ace 编辑器中动态自动完成

mongodb - 什么时候使用嵌入式文档?

c# - InvalidOperationException : Can't compile a NewExpression with a constructor declared on an abstract class

mongodb - 无法连接到 EC2 上运行的 Mongodb

mongodb - 在 MongoDB 中存储 Analytics 数据有哪些好方法?

node.js - 可能不存在的字段上的“orderBy”会完全跳过文档

node.js - Angular 9 SSR 404 未找到带有状态代码的页面

javascript - Express js apps.local 变量和 View 输出

node.js - Ember : records saved to DB but ID not being sent to Ember instance Object

node.js - 在两个文件之间传递连接 - azure 函数