Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 通过忽略数组中的重复项来计数文档_Mongodb_Mongodb Query_Nosql - Fatal编程技术网

Mongodb 通过忽略数组中的重复项来计数文档

Mongodb 通过忽略数组中的重复项来计数文档,mongodb,mongodb-query,nosql,Mongodb,Mongodb Query,Nosql,每次预约都有一些服务。我要查找每个服务的预约总数 我的文档如下所示: [{ "appointmentTag":"xyz", "services" : [ { "name" : "service1", "estimatedTime":20 }, { "name" : "service2", "estimatedTime":40 } ] }, { "appointmentTag":"abc", "se

每次预约都有一些服务。我要查找每个服务的预约总数

我的文档如下所示:

[{
  "appointmentTag":"xyz",
  "services" : [
    {
      "name" : "service1",
      "estimatedTime":20
    },
    {
      "name" : "service2",
      "estimatedTime":40
    }
  ]
},
{
  "appointmentTag":"abc",
  "services" : [
    {
      "name" : "service2",
      "estimatedTime":100
    },
    {
      "name" : "service2",
      "estimatedTime":30
    }
  ]
}
   db.test.aggregate([
  {
    "$unwind": "$services"
  },
  {
    "$group": {
      "_id": "$services.name",
      "appointments": {
        "$addToSet": "$appointmentTag"
      }
    }
  },
  {
    "$project": {
      "ServiceName": "$_id","_id":0,
      "countOfAppointments": {
        "$size": "$appointments"
      }
    }
  }
])
预期产出如下:

{"serviceName":"service1", "count":1}
{"serviceName":"service2", "count":2}
我尝试的查询:

db.appointments.aggregate([
    {"$unwind": "$services"},
    {"$group": {
        "_id": "$services.name",
        "count": {"$sum":1}
    }}
]);
获得以下结果:

{"serviceName":"service1", "count":1}
{"serviceName":"service2", "count":3}

你做得几乎正确。展开后,当您按serviceName分组而不是计数时,请使用$addToSet of appointmentTag,然后计数该数组。大概是这样的:

[{
  "appointmentTag":"xyz",
  "services" : [
    {
      "name" : "service1",
      "estimatedTime":20
    },
    {
      "name" : "service2",
      "estimatedTime":40
    }
  ]
},
{
  "appointmentTag":"abc",
  "services" : [
    {
      "name" : "service2",
      "estimatedTime":100
    },
    {
      "name" : "service2",
      "estimatedTime":30
    }
  ]
}
   db.test.aggregate([
  {
    "$unwind": "$services"
  },
  {
    "$group": {
      "_id": "$services.name",
      "appointments": {
        "$addToSet": "$appointmentTag"
      }
    }
  },
  {
    "$project": {
      "ServiceName": "$_id","_id":0,
      "countOfAppointments": {
        "$size": "$appointments"
      }
    }
  }
])

你做得几乎正确。展开后,当您按serviceName分组而不是计数时,请使用$addToSet of appointmentTag,然后计数该数组。大概是这样的:

[{
  "appointmentTag":"xyz",
  "services" : [
    {
      "name" : "service1",
      "estimatedTime":20
    },
    {
      "name" : "service2",
      "estimatedTime":40
    }
  ]
},
{
  "appointmentTag":"abc",
  "services" : [
    {
      "name" : "service2",
      "estimatedTime":100
    },
    {
      "name" : "service2",
      "estimatedTime":30
    }
  ]
}
   db.test.aggregate([
  {
    "$unwind": "$services"
  },
  {
    "$group": {
      "_id": "$services.name",
      "appointments": {
        "$addToSet": "$appointmentTag"
      }
    }
  },
  {
    "$project": {
      "ServiceName": "$_id","_id":0,
      "countOfAppointments": {
        "$size": "$appointments"
      }
    }
  }
])