Mongodb Aggregation count array/set size

前端 未结 3 1544
花落未央
花落未央 2021-02-05 18:07

Here\'s my problem:

Model:

{ application: \"abc\", date: Time.now, status: \"1\" user_id: [ id1, id2, id4] }

{ application: \"abc\",

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 18:45

    Sorry I'm a little late to the party. Simply grouping on the 'user_id' and counting the result with a trivial group works just fine and doesn't run into doc size limits.

    [
        {$match: {application: 'abc', date: {$gte: startDate, $lte: endDate}}},
        {$unwind: '$user_id'},
        {$group: {_id: '$user_id'}},
        {$group: {_id: 'singleton', count: {$sum: 1}}}
    ];
    

提交回复
热议问题