Mongodb找到了具有条件的独特计数

时间:2017-07-06 08:39:31

标签: mongodb

如何在mongodb中选择。

Select room(distinct), count(where read =1)
from chat
where from = "1" or to ="1"

这是我的json

{
    "_id" : ObjectId("595da6052008fc2213db32f6"),
    "room" : "1_40",
    "from" : "1",
    "to" : "40",
    "user_name" : "Tran Cot",
    "mes" : "hgfd",
    "time" : 1499309573832,
    "read" : 1
}

1 个答案:

答案 0 :(得分:0)

如果您想要每个房间的总阅读信息..

使用aggregation

更新

db.chat.aggregate([
  { $match: { $or: [{ from: "41" }, { to: "41" }] } }, 
  { $group: { 
    _id: "$room", 
    count: {
      $sum: {
        $cond: { if: $eq: ["$read", false], then: 1, else: 0 }
      }
    }
  }}
]);
相关问题