MongoDB aggregate by field exists

前端 未结 6 1449
不思量自难忘°
不思量自难忘° 2020-12-02 12:33

I have a hard time believing this question hasn\'t been asked and answered somewhere already, but I can\'t find any trace of it.

I have a MongoDB aggregation query t

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 12:33

    I solved the same problem just last night, this way:

    > db.test.aggregate({$group:{_id:{$gt:["$field", null]}, count:{$sum:1}}})
    { "_id" : true, "count" : 2 }
    { "_id" : false, "count" : 2 }
    

    See http://docs.mongodb.org/manual/reference/bson-types/#bson-types-comparison-order for a full explanation of how this works.

提交回复
热议问题