0

I have to return date from string(which is of format dd/mm/YYYY) in mongodb. My code :

db.system.js.save({
    _id:"testdate",
value:function(stringDate){
        return new Date(Date.parseDate(stringDate,'d/m/Y'))
    }
}) ;

I am getting exception as Date.parseDate is not a function. Is there any other way to convert string to date?

Nikesh Kedlaya
  • 652
  • 4
  • 10
  • 30
  • What do you think you are saving this to `system.js` for? You really want this to be regular code in the language you are implementing in. Which is what exactly? Your question history seems to suggest Java. – Neil Lunn Oct 23 '17 at 04:40
  • @NeilLunn i am just testing the code. Actually i will use the code in insert statement. I am getting a string date from client, i will parse it and insert. – Nikesh Kedlaya Oct 23 '17 at 04:41
  • My comment is to make it obvious that you actually do not want to save in `system.js` and should be using your native language instead. Since you don't seem to be listening though .... – Neil Lunn Oct 23 '17 at 04:43
  • I understand what you are saying but i can anyways call this function from my native language. This function will take some more data and some json conversion so i am using system.js to do my work – Nikesh Kedlaya Oct 23 '17 at 04:46
  • That's not what stored JavaScript functions in MongoDB are for. So you don't seem to understand at all. Plenty of material out there on why this is so. Also be aware of the deprecation notice on [eval.](https://docs.mongodb.com/manual/reference/command/eval/) which is near due removal completely. – Neil Lunn Oct 23 '17 at 04:48

0 Answers0