MongoDB + Node.js: it is not possible to enter the date correctly

I used node-mongoskin to connect these two. Everything was fine until I requested some kind of "date" field, which, I think, should be returned as a Date javascript object. But the result type was a string that is odd (for me) and uncomfortable.

The insert looks something like this:

 var doc = { date: new Date(), info: 'Some info' } db.users.insert( doc, {safe: true}, function(err, res) { ... }); 

And the result above (without _id field):

 { "date" : "Mon Oct 24 2011 18:00:57 GMT+0400 (MSK)", "info": "Some info" } 

However, pasting with MongoDB Shell works just fine, except for the ISODate field ISODate

 > db.things.insert({ date: new Date() }); db.things.find(); { "_id" : ObjectId("4eae9f2a34067b92db8deb40"), "date" : ISODate("2011-10-31T13:14:18.947Z") } 

So, the question arises: how should documents be inserted into request date fields as a Date object? I want to set fields on the server side of the database. I just send something like null fields, and db-server sets for me those that use mongo mechanisms by default.

Inserting timestamps (like MongoDB's native mark ) is also a problem, but it is not so important.

PS: No luck going through mongoskin and mongodb-native docs.

+7
source share
3 answers

It was probably an error in my code or in the mongo driver. Now everything works fine:

 db.collection.insert({d: new Date()}); 

Timestamp support described here: http://mongodb.github.com/node-mongodb-native/api-bson-generated/timestamp.html .

+12
source

ISODate is a native way to store mongo. I am using the node -mongodb-native npm module and I am storing / receiving javascript Date using the new date (), as in your examples. I don't know if this was a recent correction because I started using node and Mongo in 2012, but using the date was pretty simple for me.

0
source

JavaScript Code:

  collection.insert({"className" : "models.Action", "title" : "Email", "description" : "How are you today?", "creationDate" : new Date("Fry, 4 May 2012 10:30:08 +0200 (CEST)"), "creator" : dbref }, 

created in mongoDB

 db.action.find({"title":"Email"}) > db.action.find({"title":"Email"}) { "className" : "models.Action", "title" : "Email", "description" : "How are you today?", "creationDate" : ISODate("2012-05-04T08:30:08Z"), "creator" : { "$ref" : "person", "$id" : ObjectId("4f995e4824ac8d68f63adf69") }, "_id" : ObjectId("4fa79e2e92c2a19a09000002") } 
0
source

All Articles