2017-08-15 25 views
0

ユーザーが好きだったコンテンツの合計を取得しようとしました、共有またはコメント。MongoDbは3つのフィールドの数/合計を集計します

私のコンテンツモデルは以下の構造を持っています。

Content = { 
    _id: ObjectId(), 
    author: ObjectId(), 
    value: <string of post content> 
    tags: [<trings>], 
    likes: [array of user ids], 
    shares: [array of user ids], 
    comments: [{ 
    ContentId(of the same schema), 
    User Id 
    }] 
} 

さて、IDは、コンテンツの文書を超える集約 と、このような結果を取得したいです。コンテンツがある時はいつでも

{ 
    likes: 20, 
    shares: 10, 
    comments: 5 
} 

Soは、短期では、ユーザIDが好き配列である は、 好きは株式とコメントのための同じ1

インクリメントします。

これが集約フレームワークで可能かどうかはわかりません。 私はそう思わないかもしれませんが、多分mongodbの達人がもっとよく知っているかもしれません。

クイック編集。部分的に提出された最初の投稿に基づいて、私はこれを作った。 は今、動作しているようですが、それはあまりにも簡単なようですので、イム必ず落とし穴のいくつかの並べ替えは、不足していることイムがある:)

db.getCollection('contents').aggregate([ 


{$facet: { 
     "likes": [ 
     {$match: {likes: ObjectId("596537d6b63edc2318ee9f0c")} }, 
     {$group: { 
      _id : "$likes", 
      count: { $sum: 1 } 
     }}, 
     { $project: { count: 1} } 
     ], 
     "shares": [ 
     {$match: {shares: ObjectId("596537d6b63edc2318ee9f0c")} }, 
     {$group: { 
      _id : "$shares", 
      count: { $sum: 1 } 
     }}, 
     { $project: { count: 1} } 
     ], 
     "posts": [ 
     {$match: {$and: [ 
      {user: ObjectId("596537d6b63edc2318ee9f0c")}, 
      {parent: {$exists: false} } 
     ]} }, 
     {$group: { 
      _id : "$_id", 
      count: { $sum: 1 } 
     }}, 
     { $project: { count: 1} } 
     ] 
    } 
}]); 

することができます上記のコードで間違った場所に何か?

答えて

0

3.4バージョンで以下の集計を試すことができます。

以下のクエリは$groupすべてのドキュメントになります。が配列内にあるかどうかを確認するには、$inオペレータを使用してください。 user_idが他に発見されたすべての文書を超える集約する0$sumを設定されている場合

likesshares配列の場合、1を使用しています。

commentsの場合、配列の配列であるため2段階の処理です。コンテンツ文書内のuser_id年代の上に移動して、一致した場合、他0各要素における入力user_idと出力1をチェックするステップをINGの

$group

commentsは、グループステージの後に配列値[[1, 0], [1], [1]]の配列を持ちます。次のステップは、$reduce演算子を使用して、すべての値を合計してcommentsカウントにすることです。

db.collection_name.aggregate([ 
    { 
    "$group": { 
     "_id": null, 
     "likes": { 
     "$sum": { 
      "$cond": [ 
      { 
       "$in": [ 
       user_id, 
       "$likes" 
       ] 
      }, 
      1, 
      0 
      ] 
     } 
     }, 
     "shares": { 
     "$sum": { 
      "$cond": [ 
      { 
       "$in": [ 
       user_id, 
       "$shares" 
       ] 
      }, 
      1, 
      0 
      ] 
     } 
     }, 
     "comments": { 
     "$push": { 
      "$map": { 
      "input": "$comments", 
      "as": "comment", 
      "in": { 
       "$cond": [ 
       { 
        "$in": [ 
        user_id, 
        "$$comment.user_id" 
        ] 
       }, 
       1, 
       0 
       ] 
      } 
      } 
     } 
     } 
    } 
    }, 
    { 
    "$addFields": { 
     "comments": { 
     "$reduce": { 
      "input": "$comments", 
      "initialValue": 0, 
      "in": { 
      "$add": [ 
       "$$value", 
       { 
       "$sum": "$$this" 
       } 
      ] 
      } 
     } 
     } 
    } 
    } 
]) 
0
db.collection.aggregate([ 
    { 
    $facet: { 
     "LikeCategory": [ 
     { $unwind: "$likes" }, 
     { $group : { 
      _id : "$likes", 
      count: { $sum: 1 } 
     } 
     },{ $project : { 
      userId : "$_id" 
      _id : 0, 
      count : 1 

     }} 
     ], 
     "ShareCategory": [ 
     { $unwind: "$shares" }, 
     { $group : { 
      _id : "$shares", 
      count: { $sum: 1 } 
     } 
     },{ $project : { 
      userId : "$_id" 
      _id : 0, 
      count : 1 

     }} 
     ], 
     "CommentCategory": [ 
     { $unwind: "$comments" }, 
     { $group : { 
      _id : "$comments.userId", 
      count: { $sum: 1 } 
     } 
     },{ $project : { 
      userId : "$_id" 
      _id : 0, 
      count : 1 

     }} 
     ] 
} 
} 
]; 

このようにして、カウントを調べることができます。上記のコードには構文上の問題があるかもしれませんが、あなたの問題を解決できることを願っています。