MongoDB $group 和 $project

问题描述 投票:0回答:1

我需要找到共享同一车牌号的司机的姓名、Dob、地址和车牌号。这是我使用的代码。

db.employee.aggregate([
  {
    $group: {
      _id: {
        "license": "$EMPLOYEE.licence"
      },
      count: {
        $sum: 1
      }
    }
  },
  {
    $match: {
      count: {
        "$gt": 1
      }
    }
  },
  {
    $project: {
      "EMPLOYEE.name": 1,
      "EMPLOYEE.dob": 1,
      "EMPLOYEE.address": 1,
      "EMPLOYEE.licence": 1
    }
  }
])

所以这个可以用,但它只显示重复的驾照号码。它不显示$project部分。我做错了什么?这是样本数据,如果你想测试它。

db={
  "employee": [
    {
      "_id": "6",
      "EMPLOYEE": {
        "e#": "6",
        "name": "Michael Jones",
        "dob": "05-OCT-65",
        "address": "23 Waterloo Ave. Surry Hills, NSW 2502",
        "hiredate": "12-JAN-93",
        "position": "mechanic",
        "licence": "7773",
        "status": "ON_LEAVE",
        "experience": "STANDARD",
        "maintenances": [
          {
            "registration": "QRT834",
            "time": "40",
            "maintenance date": "12-JUN-99"
          },
          {
            "registration": "QRT834",
            "time": "40",
            "maintenance date": "15-JUN-98"
          },
          {
            "registration": "SYF777",
            "time": "30",
            "maintenance date": "01-AUG-98"
          }
        ]
      }
    },
    {
      "_id": "9",
      "EMPLOYEE": {
        "e#": "9",
        "name": "Alex Davis",
        "dob": "15-AUG-93",
        "address": "23 Waterloo Ave. Surry Hills, NSW 2502",
        "hiredate": "12-JAN-93",
        "position": "mechanic",
        "licence": "9385",
        "status": "ON_LEAVE",
        "experience": "STANDARD",
        "maintenances": [
          {
            "registration": "QRT834",
            "time": "40",
            "maintenance date": "30-JUN-02"
          }
        ]
      }
    },
    {
      "_id": "23",
      "EMPLOYEE": {
        "e#": "23",
        "name": "Rose Mathews",
        "dob": "20-NOV-80",
        "address": "23 Waterloo Ave. Surry Hills, NSW 2502",
        "hiredate": "12-JAN-93",
        "position": "mechanic",
        "licence": "7773",
        "status": "ON_LEAVE",
        "experience": "STANDARD",
        "maintenances": [
          {
            "registration": "QRT834",
            "time": "40",
            "maintenance date": "12-JUN-99"
          },
          {
            "registration": "LUCY01",
            "time": "200",
            "maintenance date": "12-MAR-97"
          },
          {
            "registration": "QRT834",
            "time": "40",
            "maintenance date": "30-JUN-02"
          }
        ]
      }
    }
  ]
}

我在这个网站上运行它。https:/mongoplayground.net。

mongodb nosql aggregate
1个回答
0
投票

你需要将这些字段从 $group 阶段也是如此。

我已经更新了查询,请看一下。

db.sample.aggregate([
  {
    $group:{
      "_id":"$EMPLOYEE.licence",
      "count":{
        $sum:1
      },
      "EMPLOYEE":{
        $addToSet:{
        "name":"$EMPLOYEE.name",
        "dob":"$EMPLOYEE.dob",
        "address":"$EMPLOYEE.address",
        "licence":"$EMPLOYEE.licence",
        }
      }
    }    
  },
  {
    $match:{
      count:{
        $gt:1
      }
    }
  },
  {
    $unwind:"$EMPLOYEE"
  },
  {
    $project:{
     "EMPLOYEE.name": 1,
     "EMPLOYEE.dob": 1,
     "EMPLOYEE.address": 1,
     "EMPLOYEE.licence": 1
    }
  }
]).pretty()

输出

{
        "_id" : "7773",
        "EMPLOYEE" : {
                "name" : "Michael Jones",
                "dob" : "05-OCT-65",
                "address" : "23 Waterloo Ave. Surry Hills, NSW 2502",
                "licence" : "7773"
        }
}
{
        "_id" : "7773",
        "EMPLOYEE" : {
                "name" : "Rose Mathews",
                "dob" : "20-NOV-80",
                "address" : "23 Waterloo Ave. Surry Hills, NSW 2502",
                "licence" : "7773"
        }
}

希望能帮到你:)

© www.soinside.com 2019 - 2024. All rights reserved.