删除除第一个索引外的mongodb数组键

时间:2018-11-28 10:57:57

标签: mongodb mongodb-query aggregation-framework

我有一个像这样的mongo db数据。

{
  "_id": "1234",
  "programId": ObjectId("1234"),
  "userId": "23",
  "courses": [
    {
      "stateName": "Stage 1",
      "duration": "5",
      "lag": "2",
      "courseType": "3",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2018-12-01T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100,

    },
    {
      "stateName": "2nd stage",
      "duration": "60",
      "lag": "60",
      "courseType": "2",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-02-01T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "3rd Stage",
      "duration": "5",
      "lag": "2",
      "courseType": "1",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-04-07T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "4th stage",
      "duration": 1,
      "lag": "10",
      "courseType": "1",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-04-10T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "5th",
      "duration": "1",
      "lag": "0",
      "courseType": "3",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-04-21T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "6th",
      "duration": "5",
      "lag": "5",
      "courseType": "3",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-04-26T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "7th ",
      "duration": 1,
      "lag": "9",
      "courseType": "3",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-05-02T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "8th",
      "duration": "66",
      "lag": 0,
      "courseType": "3",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-07-16T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "9th",
      "duration": "61",
      "lag": 0,
      "courseType": "1",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-09-15T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "10th",
      "duration": "80",
      "lag": 0,
      "courseType": "3",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-12-04T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    },
    {
      "stateName": "11th",
      "duration": "8",
      "lag": 0,
      "courseType": "3",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2019-12-12T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100
    }
  ],
  "userStatus": 1,
  "modified": ISODate("2018-11-27T14:01:32.082+05:30"),
  "created": ISODate("2018-11-27T14:01:32.082+05:30"),
  "completionStatus": "F",
  "currentState": {
    "courseId": "116",
    "courseProgress": 100
  }
}

除了第一个索引之外,我想从课程数组中删除cheduledStartDate,cheduledEndDate,courseProgress。

我想要这样的结果

{
  "_id": "1234",
  "programId": ObjectId("1234"),
  "userId": "23",
  "courses": [
    {
      "stateName": "Stage 1",
      "duration": "5",
      "lag": "2",
      "courseType": "3",
      "scheduledStartDate": ISODate("2018-11-27T14:01:30.000+05:30"),
      "scheduledEndDate": ISODate("2018-12-01T14:01:30.000+05:30"),
      "transitionType": "onAssignment",
      "courseProgress": 100,

    },
    {
      "stateName": "2nd stage",
      "duration": "60",
      "lag": "60",
      "courseType": "2",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "3rd Stage",
      "duration": "5",
      "lag": "2",
      "courseType": "1",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "4th stage",
      "duration": 1,
      "lag": "10",
      "courseType": "1",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "5th",
      "duration": "1",
      "lag": "0",
      "courseType": "3",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "6th",
      "duration": "5",
      "lag": "5",
      "courseType": "3",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "7th ",
      "duration": 1,
      "lag": "9",
      "courseType": "3",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "8th",
      "duration": "66",
      "lag": 0,
      "courseType": "3",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "9th",
      "duration": "61",
      "lag": 0,
      "courseType": "1",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "10th",
      "duration": "80",
      "lag": 0,
      "courseType": "3",
      "transitionType": "onAssignment",
    },
    {
      "stateName": "11th",
      "duration": "8",
      "lag": 0,
      "courseType": "3",
      "transitionType": "onAssignment",
    }
  ],
  "userStatus": 1,
  "modified": ISODate("2018-11-27T14:01:32.082+05:30"),
  "created": ISODate("2018-11-27T14:01:32.082+05:30"),
  "completionStatus": "F",
  "currentState": {
    "courseId": "116",
    "courseProgress": 100
  }
}

1 个答案:

答案 0 :(得分:1)

使用$range聚合查找数组courses的索引,然后使用$map聚合遍历数组长度并删除("$$REMOVE")索引不相等的字段{ {3}}到 0

对于mongodb 3.6 及更高版本

db.collection.aggregate([
  { "$addFields": {
    "courses": {
      "$map": {
        "input": { "$range": [0, {"$size": "$courses" }] },
        "in": {
          "$let": {
          "vars": { "c": {"$arrayElemAt": ["$courses", "$$this"]} },
            "in": {
              "stateName": "$$c.stateName",
              "duration": "$$c.duration",
              "lag": "$$c.lag",
              "courseType": "$$c.courseType",
              "scheduledStartDate": { "$cond": [{ "$ne": ["$$this", 0] }, "$$REMOVE", "$$c.scheduledStartDate"] },
              "scheduledEndDate": "$$c.scheduledEndDate",
              "scheduledEndDate": { "$cond": [{ "$ne": ["$$this", 0] }, "$$REMOVE", "$$c.scheduledEndDate"] },
              "transitionType": "$$c.transitionType"
            }
          }
        }
      }
    }
  }}
])

对于 3.6

之前的mongodb
db.collection.aggregate([
  { "$addFields": {
    "courses": {
      "$map": {
        "input": { "$range": [0, { "$size": "$courses" }] },
        "in": {
          "$let": {
            "vars": { "c": { "$arrayElemAt": ["$courses", "$$this"] }},
            "in": {
              "$cond": [
                { "$eq": ["$$this", 0] },
                {
                  "stateName": "$$c.stateName",
                  "duration": "$$c.duration",
                  "lag": "$$c.lag",
                  "courseType": "$$c.courseType",
                  "scheduledStartDate": "$$c.scheduledStartDate",
                  "scheduledEndDate": "$$c.scheduledEndDate",
                  "stateName": "$$c.stateName",
                  "transitionType": "$$c.transitionType"
                },
                {
                  "stateName": "$$c.stateName",
                  "duration": "$$c.duration",
                  "lag": "$$c.lag",
                  "courseType": "$$c.courseType",
                  "transitionType": "$$c.transitionType"
                }
              ]
            }
          }
        }
      }
    }
  }}
])

($ne)

[
  {
    "_id": "1234",
    "completionStatus": "F",
    "courses": [
      {
        "courseProgress": 100,
        "courseType": "3",
        "duration": "5",
        "lag": "2",
        "scheduledEndDate": ISODate("2018-12-01T08:31:30Z"),
        "scheduledStartDate": ISODate("2018-11-27T08:31:30Z"),
        "stateName": "Stage 1",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "2",
        "duration": "60",
        "lag": "60",
        "stateName": "2nd stage",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "1",
        "duration": "5",
        "lag": "2",
        "stateName": "3rd Stage",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "1",
        "duration": 1,
        "lag": "10",
        "stateName": "4th stage",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "1",
        "lag": "0",
        "stateName": "5th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "5",
        "lag": "5",
        "stateName": "6th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": 1,
        "lag": "9",
        "stateName": "7th ",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "66",
        "lag": 0,
        "stateName": "8th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "1",
        "duration": "61",
        "lag": 0,
        "stateName": "9th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "80",
        "lag": 0,
        "stateName": "10th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "8",
        "lag": 0,
        "stateName": "11th",
        "transitionType": "onAssignment"
      }
    ],
    "created": ISODate("2018-11-27T08:31:32.082Z"),
    "currentState": {
      "courseId": "116",
      "courseProgress": 100
    },
    "modified": ISODate("2018-11-27T08:31:32.082Z"),
    "userId": "23",
    "userStatus": 1
  }
]