Mongodb: inserting record with date as ISODate

1,380 views
Skip to first unread message

mfeb

unread,
May 10, 2016, 11:45:34 AM5/10/16
to Node-RED
Another go at a familiar problem:

Is there any way using the Node-RED mongodb nodes to insert a record that contains a date and appears in the database as ISODate(...)?

The standard way in the mongo shell is to use either 1) the "new Date(...)" construct, but this is construct is disallowed by the mongodb node due to it not being strict json format, or 2) a {"date_field" : {'date" : datestring}}, which is legal json but is not reflected in the db as "date_field" : ISODate(datestring).

It appears that in most/all mongo drivers there's a way to opt for the shell interpreter, which does not require strict json.

Is there another way from within node-RED to send something off to mongo and have the date be other than a date string?

Mark

Andreas Nösberger

unread,
Jan 3, 2017, 5:27:58 AM1/3/17
to Node-RED
Hi, did you found a solution for the problem. I'm having exactly the same problem at the moment. The date is stored as string in mongodb but should be ISODate(Value).

Andreas Nösberger

unread,
Jan 3, 2017, 9:01:11 AM1/3/17
to Node-RED
Hi

I've just created a function node with the following code:
msg.payload.date = new Date(msg.payload.date);
return msg;

This is then storing the date as ISODate in the MongoDB:
"date" : ISODate("2017-01-03T13:59:31Z")

Julian Knight

unread,
Jan 3, 2017, 12:36:49 PM1/3/17
to Node-RED
Or use one of the date formatting nodes if you don't want to write function code.

Nicholas O'Leary

unread,
Jan 3, 2017, 12:59:29 PM1/3/17
to Node-RED

> Or use one of the date formatting nodes if you don't want to write function code.

No. This is specifically about passing a JavaScript Date object to the mongo node, not formatting it as a String.


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/b3711852-bb83-43f5-b834-f7d8c9bc2657%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages