donglong1465 2016-10-18 12:46
浏览 59
已采纳

如何对嵌入文档进行聚合查询?

I am using jenssegers/laravel-mongodb library in a laravel application however I need to show counts of an embedded document. Using a generic example of comment/posts, while I can solve my problem by just pulling all the posts and looping through to get comments to count them but was just was not sure if I could query them.

I did set up my relationships. In my post class I did:

 public function comments()
    {
        return $this->hasMany('App\Comment');
    }

and in my comment class:

 public function post()
        {
            return $this->belongsTo('App\Post');
        }

Later in code:

 $post->comments()->save($comment);
 $comment->post()->associate($post);

my document structure:

"posts" : [ 
{
    "_id" : ObjectId("5805a11e2594ee26543ea041"),
    "Post_Num" : "166236001010",
    "updated_at" : ISODate("2016-10-18T04:12:14.454Z"),
    "created_at" : ISODate("2016-10-18T04:12:14.451Z"),
    "comments" : [ 
        {   
            "Comment_Num" : "3333333",
            "_id" : ObjectId("5805a11e2594ee26543ea042"),
            "post_id" : "5805a11e2594ee26543ea041",
        },
        {   
            "Comment_Num" : "3333333",
            "_id" : ObjectId("5805a11e2594ee26543ea042"),
            "post_id" : "5805a11e2594ee26543ea041",
        }
    ]

},

{
    "_id" : ObjectId("5805a11e2594ee26543ea041"),
    "Post_Num" : "166236001010",
    "comments" : [ 
        {   
            "Comment_Num" : "3333333",
            "_id" : ObjectId("5805a11e2594ee26543ea042"),
            "post_id" : "5805a11e2594ee26543ea041",
        }
    ]

}
]

Now when I try getting the comments like:

$post->comments()->count() 
or
$post->comments()->get()->count() 
or
$post->comments->get()->count() 

I get a 0. The same logic works if it is not an embedded document but just was wondering if it was possible to do an aggregate query ? Perhaps is best to just let the code iterate and add everything?

As you can tell I need some minor hand holding. Thank You

UPDATE: I am trying the following

 public function commentCount()
    {

        $commentsCount = Post::raw(function($collection)
        {

            return $collection->aggregate(['$project' => ['_id' => 1,
                'comments_count' => ['$size' => '$comments']],
                ['$group' => ['_id' => null, 'count' => ['$sum' => '$comments_count']]]]);

        });
        return $commentsCount;
    }

What I get now is:

$pipeline is not a list (unexpected index: "$project") 
  • 写回答

1条回答 默认 最新

  • dongshui9690 2016-10-18 14:16
    关注

    Just to be clear, you want a list of your posts with the number of comments on each post?

    Aggregation has something to offer for that: https://docs.mongodb.com/manual/reference/operator/aggregation/size/#exp._S_size

    I'm not a php dev but this is my shot at it:

    Post::raw()->aggregate(
        ['$project' => ['_id' => 1,
                        'Post_Num' => 1,
                        'comments_count' => ['$size' => '$comments']],
        ['$group' => ['_id' => null, 'count' => ['$sum' => '$comments_count']]]
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误