0

I have previously asked a question about converting string to dates - Convert Date to String in nested array in mongodb - and got a great and working answer. The only issue is that when a date is a blank -> '' then I get the following error:

{
    "message" : "an incomplete date/time string has been found, with elements missing: ' '",
    "ok" : 0,
    "code" : 241,
    "codeName" : "ConversionFailure",
    "name" : "MongoError"
}

the formula Im using (taken from question) is:

db.cases.aggregate([
  { $match: { companyID: 218 }},
  { "$addFields": {
    "cases": {
      "$map": {
        "input": "$cases",
        "in": {
          "$mergeObjects": [
            "$$this",
            {
              "createddate": {
                "$dateFromString": { "dateString": "$$this.createddate" }
              },
              "endDate": {
                "$dateFromString": { "dateString": "$$this.endDate" }
              }
            }
          ]
        }
      }
    }
  }}
])

I have tried adding in

{ $unwind: "$cases" },
{ $match: { 'cases.endDate': {'$ne' : ''}}},

But that gave me an error saying:

{
    "message" : "input to $map must be an array not object",
    "ok" : 0,
    "code" : 16883,
    "codeName" : "Location16883",
    "name" : "MongoError"
}

Any help would be appreciated.

Thank you

0

1 Answer 1

1

You have to do $group after $unwind to get $cases back in array form

{ $unwind: "$cases" },
{ $match: { 'cases.endDate': {'$ne' : ''}}},
{ $group: {
  _id:$null,
  cases:{$push:'$cases'}
} },
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.