Insert Date into MongoDB

When I try insert a date filed in MongoDB it is inserting as null.

actually the date should insert like as follows

"createdDate": {
        "$date": "2021-03-09T09:04:52.794Z"
    },

but it is inserting as

"createdDate":null

Can some one help me to insert a date filed into MongoDB.

Thanks in Advance.

Hello, can you share your current workflow?

Here is the workflow

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        160,
        280
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "createdDate",
              "value": "2019-10-07T18:30:00.000Z"
            },
            {
              "name": "name",
              "value": "Adam Smith"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        310,
        280
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "User",
        "fields": "name,createdDate"
      },
      "name": "MongoDB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        510,
        280
      ],
      "credentials": {
        "mongoDb": "mongo-cred"
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "MongoDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

It’s working just fine for me. Not sure what might be happening there. Is the date field nested?

Can you share your schema?

The schema is already defined using some other application. And in the mongoDB schema definition, the createdDate property datatype defined as Date type. So its getting inserting as null.

Hi @RicardoE105 actually the date should insert like as follows

"createdDate": {
        "$date": "2021-03-09T09:04:52.794Z"
    },

but it is inserting as

"createdDate":null

Ahh yes, I tested with a string type. That is why it worked for me. Checking again

Hi @RicardoE105

Any update on this

Ahh yes, had to make some changes to make work. Will send the PR today. We will try to add to the next released.

Ok, added a new option field called “Date Fields”. Set the the Date Fields and it’s gonna be set to a instance of Date. Can you test it locally and provide feedback?

https://github.com/n8n-io/n8n/pull/1529

Got released with [email protected]

Thanks a lot @jan for prompt response