我有个Json:
[
  {
    "key": "guitar",
    "name": "john"
  },
  {
    "key": "guitar",
    "name": "paul"
  },
  {
    "key": "guitar",
    "name": "george"
  },
  {
    "key": "drums",
    "name": "ringo"
  }
]让我们按key分组并将项添加到每个组中:
db.collection.aggregate([
  {
    $sort: {
      name: -1
    }
  },
  {
    $group: {
      _id: "$key",
      "projects": {
        "$push": "$$ROOT"
      },
      count: {
        $sum: 1
      }
    }
  },
  {
    $sort: {
      "_id": 1
    }
  }
])结果:
[
  {
    "_id": "drums",
    "count": 1,
    "projects": [
      {
        "_id": ObjectId("5a934e000102030405000003"),
        "key": "drums",
        "name": "ringo"
      }
    ]
  },
  {
    "_id": "guitar",
    "count": 3,
    "projects": [
      {
        "_id": ObjectId("5a934e000102030405000001"),
        "key": "guitar",
        "name": "paul"
      },
      {
        "_id": ObjectId("5a934e000102030405000000"),
        "key": "guitar",
        "name": "john"
      },
      {
        "_id": ObjectId("5a934e000102030405000002"),
        "key": "guitar",
        "name": "george"
      }
    ]
  }
]问题:
如何添加这样的计数器:
[
  {
    "_id": "drums",
    "Counter":0,       // <--------------------------------
    "count": 1,
    "projects": [
      {
        "_id": ObjectId("5a934e000102030405000003"),
        "key": "drums",
        "name": "ringo",
        "InnerCounter":0       // <--------------------------------
        
      }
    ]
  },
  {
    "_id": "guitar",
    "Counter":1,       // <--------------------------------
    "count": 3,
    "projects": [
      {
        "_id": ObjectId("5a934e000102030405000001"),
        "key": "guitar",
        "name": "paul",
        "InnerCounter":0       // <--------------------------------
      },
      {
        "_id": ObjectId("5a934e000102030405000000"),
        "key": "guitar",
        "name": "john",
        "InnerCounter":1       // <--------------------------------
      },
      {
        "_id": ObjectId("5a934e000102030405000002"),
        "key": "guitar",
        "name": "george",
        "InnerCounter":2       // <--------------------------------
      }
    ]
  }
]发布于 2020-09-01 15:01:09
我认为没有其他选项可以这样做,您可以尝试使用$unwind来使用includeArrayIndex属性在数组中创建索引字段,
db.collection.aggregate([
  { $sort: { name: -1 } },
  {
    $group: {
      _id: "$key",
      projects: { $push: "$$ROOT" }
    }
  },
  // $unwind "projects" array and store index in field "projects.InnerCounter"
  {
    $unwind: {
      path: "$projects",
      includeArrayIndex: "projects.InnerCounter"
    }
  },
  // again group by _id and reconstruct "projects" array
  {
    $group: {
      _id: "$_id",
      projects: { $push: "$projects" },
      count: { $sum: 1 }
    }
  },
  // sort by "_id"
  { $sort: { "_id": 1 } },
  // group by $$ROOT and construct root array 
  {
    $group: {
      _id: null,
      root: { $push: "$$ROOT" }
    }
  },
  // deconstruct root array and add field "Counter" ofr index counter
  {
    $unwind: {
      path: "$root",
      includeArrayIndex: "root.Counter"
    }
  },
  // replace to root
  { $replaceRoot: { newRoot: "$root" } }
])projects数组没有$unwind的另一个选项,
https://stackoverflow.com/questions/63689580
复制相似问题