繁体   English   中英

MongoDB聚合

[英]MongoDB aggregation

我有一个小问题,$ group,我需要从帖子集合中获取所有帖子标签的计数(在tags.post字段中获取计数)

我用月光ODM

发布示例模型:

 var PostSchema = new Schema({
        inc: {
            type: Number
        },
        title: {
            type: String,
            required: true
        },
        description: {
            type: String,
            required: true
        },
        tags:{
            post:[{ type: Schema.Types.ObjectId, ref: 'Tag'}],
            system:[{ type: Schema.Types.ObjectId, ref: 'Tag' }]
        }

    });
    var Post = Mongoose.model('Post', PostSchema, 'posts');

    module.exports = Post;

标签示例模型:

var TagSchema = new Schema({

    name: {
        index: { unique: true },
        type: String,
        required: true
    }
});

var Tag = Mongoose.model('Tag', TagSchema, 'tags');

module.exports = Tag;

结果应该是这样的:

[
{
 "tags_id":"12345667",
 "count": 4
},
{
 "tags_id":"12345668",
 "count": 3
},
{
 "tags_id":"12345669",
 "count": 2
},
{
 "tags_id":"12345660",
 "count": 1
}
]

在SQL数据库中,它看起来像

SELECT tag_id, COUNT(*) AS count
FROM posts
GROUP BY tag_id;

我没有经验MONGODB

这是我的尝试

Post.aggregate([
{
 $match: {
  "tags.post": {
   $ne: []
  }
 },
{
 $lookup:{
  from: "tags",
  localField: "tags.post",
  foreignField: "_id",
  as: "tags"
}
},
{ 
 $group: {
  _id: '$tags._id',
  count: {'$sum':1}
 }
}], function (err, result) {
 if (err) {
  return console.log(err);
 }
  return console.log(result);
});

结果:

[
    {
        "_id": [
            "59ad4cfe454aaf4f46f5dcea",
            "59ad4ff994190a4b8acc6871",
            "59ad4ff994190a4b8acc6872",
            "59ad65bd454aaf4f46f5dd15"
        ],
        "count": 1
    },
    {
        "_id": [
            "59ad65bd454aaf4f46f5dd15"
        ],
        "count": 1
    },
    {
        "_id": [
            "59ae20e19d094c31d3751781"
        ],
        "count": 1
    },
    {
        "_id": [
            "59ad4cfe454aaf4f46f5dcea"
        ],
        "count": 1
    },
    {
        "_id": [
            "59ad4fcb454aaf4f46f5dd02"
        ],
        "count": 1
    }
]

谢谢

只需在$group使用一个字段,如下所示,

"Count": { "$sum": 1}

这将给您计数。

我找到了解决方案:

Tag.aggregate([
            {
                $lookup: {
                    from: "posts",
                    localField: "_id",
                    foreignField: "tags.post",
                    as: "posts"
                }
            },
            {
                $unwind: "$posts"
            },
            {
                $group: {_id:"$_id", posts: {$push:"$posts"},name : { $first: '$name' }, size: {$sum:1}}
            },
            {
                $project : { name : 1, size:1 }
            },
            {
                $sort:{size:-1}
            },
            {
                $limit : limit
            }
        ], function (err, result) {
            if (err) {
                return utils.res.error(res, { message: 'Could not fetch all public tags', reason: err, debug: result })
            }
            return utils.res.ok(res, result);
        })

感谢大家,如果有人有解决方案,请在这里写下,我将不胜感激

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM