如何计算分组汇总的评分? [英] How to count rating on group aggregation?

查看:63
本文介绍了如何计算分组汇总的评分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在mongodb聚合上以获取评分计数.我已经在尝试使自己成为管道.看起来在上方

I was stuck on mongodb aggregation to get count of rating. i have already trying to make myself pipeline. looks above

数据(产品收集)

{
  "status": 200,
  "message": null,
  "data": {
    "_id": "5cc570257631a313d489ba4a",
    "media": [
      "httpsdssd",
      "dfdfd"
    ],
    "comment": [
      "5cc57f1053273c05cc60e707",
      "5cc585bf6ff7a812e0e7d9d9",
      "5cc5c654bc73b408787ffadc",
      "5cc5c6e3bc73b408787ffadd"
    ],
    "store": "5cc2c9710bc5d615781fcf8a",
    "meta": {
      "title": "Traveling Sumbar",
      "price": "150000",
      "max": 5,
      "duration": 6,
      "description": "fdf fdnf jdnf dfnkdknfkkd",
      "location": {
        "province": "Sumbar",
        "city": "Padang"
      }
    },
    "option": {
      "is_promo": false,
      "auto_delete": null
    },
    "created_at": "2019-04-28T09:19:33.233Z",
    "updated_at": "2019-04-28T15:29:39.921Z",
    "__v": 0
  }
}

关于(products_comment)的评论数据

comment data on (products_comment)

{
    "helped": [],
    "deleted_at": null,
    "_id": "5cc3276e32940613506c3848",
    "user": "5cc2c7fb0bc5d615781fcf86",
    "rating": "4",
    "body": "fdfdlfdlfkdlfkdlfkd",
    "created_at": "2019-04-26T15:44:46.224Z",
    "updated_at": "2019-04-28T16:00:48.400Z",
    "__v": 0
  },
{
    "helped": [],
    "deleted_at": null,
    "_id": "5cc3276e32940613506c3848",
    "user": "5cc2c7fb0bc5d615781fcf86",
    "rating": "4",
    "body": "fdfdlfdlfkdlfkdlfkd",
    "created_at": "2019-04-26T15:44:46.224Z",
    "updated_at": "2019-04-28T16:00:48.400Z",
    "__v": 0
  },
{
    "helped": [],
    "deleted_at": null,
    "_id": "5cc3276e32940613506c3848",
    "user": "5cc2c7fb0bc5d615781fcf86",
    "rating": "3",
    "body": "fdfdlfdlfkdlfkdlfkd",
    "created_at": "2019-04-26T15:44:46.224Z",
    "updated_at": "2019-04-28T16:00:48.400Z",
    "__v": 0
  },

我已经尝试过这样的聚合管道

I have already try make aggregation pipeline like this

{
        $lookup: {
          from: "stores",
          localField: "store",
          foreignField: "_id",
          as: "store"
        }
      },
      {
        $lookup: {
          from: "products_comment",
          localField: "comment",
          foreignField: "_id",
          as: "comment"
        }
      },
      { $unwind: "$comment" },
      {
        $project: {
          media: 1,
          "store.type": 1,
          "store.profile.address.city": 1,
          "meta.title": 1,
          "meta.price": 1,
          "comment.rating": 1
        }
      }

但结果与预期不同,我想要这样的结果

but result unlike expectation, i want result like this

 {
    "_id": "5cc570257631a313d489ba4a",
    "media": [
      "httpsdssd",
      "dfdfd"
    ],
    "comment": {
      1_rating: 0, <value of rating: count of value>
      2_rating: 3,
      3_rating: 5,
      ....,
    },
    "store": [
      {
        "type": "craft",
        "profile": {
            "address": {
               city: "Padang
            }
        }
      }
    ],
    "meta": {
      "title": "Traveling Sumbar",
      "price": "150000"
    }
  }

我该如何解决我的问题?

how i do to solve my problem ?

推荐答案

在下面的查询中,您将得到完全期望的输出:

Below Query will give you exactly expected Output :

var query = [
{
  $lookup: {
     from: "comments",
     localField: "comment",
     foreignField: "_id",
     as: "comments"
  }
},
{ $unwind: "$comments" },
{ $group : {
    _id: {
        _id: '$_id',
        rating: '$comments.rating',
        media : '$media',
        meta : '$meta',
        store : '$store'
    },
    totalRating: {$sum: 1}
 }
},
{
 $group : {
     _id : {
       _id : '$_id._id',
       media : '$_id.media',
       meta : '$_id.meta',
       store : '$_id.store'
     },
     comments : {
         $push : {
              rating : '$_id.rating',
              totalRating : '$totalRating'
          }
      }
 }
},
{
  $lookup: {
    from: "stores",
    localField: "store",
    foreignField: "_id",
    as: "store"
  }
},
{
   $project: {
        '_id' : '$_id._id',
        media : '$_id.media',
        store : '$store',
        meta : {
           title:  '$_id.meta.title',
           price : '$_id.meta.price'
        },
        comments : { "$arrayToObject": {
                    "$map": {
                        "input": "$comments",
                        "as": "el",
                        "in": {
                            "k": "$$el.rating",
                            "v": "$$el.totalRating"
                        }
                    }
                } 
        }
     }
 }
];

输出:

{
"_id" : ObjectId("5cc718715290f4ed550f5305"),
"media" : [
    "httpsdssd",
    "dfdfd"
],
"store" : [ ],
"meta" : {
    "title" : "Traveling Sumbar",
    "price" : "150000"
},
"comments" : {
    "3" : 1,
    "4" : 2
  }
}
{
   "_id" : ObjectId("5cc88d99d486568c5745e4b7"),
   "media" : [
        "maha",
        "sagar"
   ],
   "store" : [ ],
   "meta" : {
      "title" : "Sagar Sumbar",
       "price" : "15000"
    },
    "comments" : {
       "3" : 2,
       "5" : 1,
       "1" : 1
    }
  }

注意:商店数据将通过$ lookup从商店集合中获取.我没有模型/数据,所以没有输出.

NOTE: Store data will be fetched from stores collection by $lookup. I don't have a model/data so, not in the output.

这篇关于如何计算分组汇总的评分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆