8

When I save a date time in MongoDB using the following format, it shows:

Error "Unable to parse JSON" 

{
  "_id" : ObjectId("58cb759805aeeae37a56dd3d"),
  "name" : "Plutus",
  "admin" : "type 1",
  "created" : new Date()
}

Please help me to save a date time in Robomongo using MongoDB in the above format.

Error description in picture:

Error message

Thanks

Cyril Graze
  • 3,881
  • 2
  • 21
  • 27
Raheel Aslam
  • 444
  • 1
  • 9
  • 28
  • Can you include the schema definition? – GPX Mar 17 '17 at 06:06
  • yes i have define schema definition code below var mongoose = require('mongoose'); // Define our site schema var SiteSchema = new mongoose.Schema({ name: String, admin: String, created : Date }); // Export the Mongoose model module.exports = mongoose.model('Site', SiteSchema); – Raheel Aslam Mar 17 '17 at 06:17
  • Thanks; can you include this in the original post? And can you paste the original stacktrace? Yours has a typo that I'm sure isn't originally in the error. – GPX Mar 17 '17 at 06:22

5 Answers5

6

You could try using new ISODate("2017-03-17 11:59"). I know it is a little cumbersome, but this is the only date value that is guaranteed to work across all versions of Robomongo and MongoDB.

Further reading -

  1. https://github.com/Studio3T/robomongo/issues/614
  2. https://github.com/Studio3T/robomongo/issues/477
GPX
  • 3,506
  • 10
  • 52
  • 69
  • Thanks Date has been save but Like JavaScript for saving current date time. can i not use new Date() function in Robomongo.? – Raheel Aslam Mar 17 '17 at 06:47
  • I don't have access to Robomongo right now, but what version of Robomongo and MongoDB are you using? Does `Date.now()` work instead? – GPX Mar 17 '17 at 07:04
  • here is image link of problem https://ibb.co/mnVEva "mongodb": "^2.2.24", Robomongo 1.0.0 – Raheel Aslam Mar 17 '17 at 07:23
  • Can you respond to the second question as well? – GPX Mar 17 '17 at 07:23
4

Just save this JSON

{"name": "whatever", "your_cool_date": "2017-03-17 11:59"}

it will be saved as a ISO date in the database:

"your_cool_date" : ISODate("2017-03-17T10:59:00Z"),
TomoMiha
  • 1,218
  • 1
  • 14
  • 12
1

Error seems to be related the JSON syntax.

There may be a hard line break in there, try trimming the lines. Please see:

Multiline strings in JSON

Cyril Graze
  • 3,881
  • 2
  • 21
  • 27
0

Robomongo doesn't support that format, my suggestion you try MongoBooster..

MongoDB. Sure it will support format of new Date().

otherwise , you put inside date value like new Date("2017-03-17")

sathish anish
  • 463
  • 1
  • 5
  • 17
0

Use shell to insert:

db.sites.insert({ "_id" : ObjectId("58cb759805aeeae37a56dd3d"), "name" : "Plutus", "admin" : "type 1", "created" : new Date() }

yawl
  • 719
  • 6
  • 7