MongoDB select count(distinct x) on an indexed column - count unique results for large data sets

前端 未结 3 1440
感情败类
感情败类 2020-11-28 19:51

I have gone through several articles and examples, and have yet to find an efficient way to do this SQL query in MongoDB (where there are millions of rows documen

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 20:45

    db.myCollection.aggregate( 
       {$group : {_id : "$myIndexedNonUniqueField"} }, 
       {$group: {_id:1, count: {$sum : 1 }}});
    

    straight to result:

    db.myCollection.aggregate( 
       {$group : {_id : "$myIndexedNonUniqueField"} }, 
       {$group: {_id:1, count: {$sum : 1 }}})
       .result[0].count;
    

提交回复
热议问题