Удалить ключи массива mongodb, кроме первого индекса - PullRequest
0 голосов
/ 28 ноября 2018

У меня есть данные 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
  }
}

Я хочу удалить планируемыеStartDate, scheduleEndDate, 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 голосов
/ 28 ноября 2018

Используйте $range агрегацию, чтобы найти индекс массива courses, затем выполните цикл по длине массива, используя $map агрегирование и удаление ("$$REMOVE") полей, где индекс не равен ($ne) до 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"
            }
          }
        }
      }
    }
  }}
])

Для mongodb до 3.6

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"
                }
              ]
            }
          }
        }
      }
    }
  }}
])

Который вернет

[
  {
    "_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
  }
]
...