1

eg: the input is

{
    "_id" : ObjectId("5e79ae1c11344b2895797042"),
    "startDay" : "20200312",
    "endDay" : "20200314"
}

I want to get ['20200312','20200313','20200314']. What should I do ?

1
  • Do the numbers increment by 1. Do you want to store the array as a field in the document Commented Mar 24, 2020 at 9:11

1 Answer 1

1

I have used $range which returns Array of numbers between range and $range accepts only integers. So, I convert the String values of startDay and endDay to integers using $toInt

Try this query,

db.collection.aggregate([
  {
    "$project": {
      "days": {
        "$range": [
          {
            "$toInt": "$startDay"
          },
          {
            "$add": [
              {
                "$toInt": "$endDay"
              },
              1
            ]
          }
        ]
      }
    }
  }
])

Query Result

[
  {
    "_id": ObjectId("5e79ae1c11344b2895797042"),
    "days": [
      20200312,
      20200313,
      20200314
    ]
  }
]

Query Test

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.