总和&与mongodb组?

时间:2017-08-30 16:01:21

标签: mongodb mongodb-query

我需要检索mongoDB中的信息,但我不知道该怎么做:( 这是我收藏的结构部分:

[{ 
  "points" : 171, 
  "superPoints" : 2228, 
  "username" : "ammanda", 
  "posts" : [{
    "comments" : [ { "username" : "jamlabtra", "comment" : "Top" }, ... ], 
    "likes" : { "users" : ['jamlabtra', 'mrcbrandon','pauljames'],  "qtty" : 67 }, 
    "type" : "feedback"
  },{
    "comments" : [ { "username" : "mrcbrandon",  "comment" : "I liked it" }, ... ], 
    "likes" : {"users" : ['mrcbrandon','pauljames'], "qtty" : 46 },
    "type" : "suggestion"
  }], 
},{ 
  "points" : 23, 
  "superPoints" : 423, 
  "username" : "pauljames", 
  "posts" : [ {
    "comments" : [ { "username" : "jamlabtra",  "comment" : "Top" }, ... ], 
    "likes" : { "users" : ['mrcbrandon'], "qtty" : 12 }, 
    "type" : "feedback"
  }, {
    "comments" : [ { "username" : "jamlabtra", "comment" : "Cool!!" }, ... ], 
    "likes" : {"users" : ['pauljames'], "qtty" : 3 }, 
    "type" : "suggestion"
  }], 
}]

我需要这个结果(通过用户名请求):

{
  'username': 'ammanda',
  'posts': {
    'feedbackTotal': 58,
    'suggestionTotal': 6,
  },
  'likes': {
    'total': 3266,
    'likers': 32,
    'perParticipant': 3,
    'perPost': 2.17
  },
  'comments': {
    'total': 123,
    'commenters': 546,
    'perParticipant': 1.3,
    'perPost': 3.3
  },
  'points': {
    'total': 32145,
  },
}

我已经能够做一些事情了:

mongodb.collection('evaluation').aggregate([
  { $match: { username: 'ammanda' } },
  {
    $project: {
      _id: 0,
      username: 1,
      points: 1,
      posts: {
        $size: '$posts'
      },
      likes: {
        $sum: '$posts.likes.qtty'
      },
    },
  },
])

但我无法获得其他信息。你能帮帮我吗?

0 个答案:

没有答案