Reputation: 85
I have the following Javascript object, how can I convert it to BSON for insert in MongoDB (and transform those date string in ISODate)?
{ name: 'Event 4',
personsMax: 2,
begin: '2014-09-22T19:00:20.000Z',
end: '2014-09-22T21:00:20.000Z',
creator: 'Jack',
created: '2014-09-22T14:52:20.517Z',
refDate: '2014-10-08T00:00:00.000Z',
whoIsIn: { jack: true },
id: 4 }
I'm using nod.js + node-mongodb-native client.
After db.insert in mongodb I have:
db.test.find();
{ "name" : "Event 4", "personsMax" : 2, "begin" : "2014-09-22T19:00:20.000Z",
"end" : "2014-09-22T21:00:20.000Z", "creator" : "Jack",
"created" : "2014-09-22T14:52:20.517Z", "refDate" : "2014-10-08T00:00:00.000Z",
"whoIsIn" : { "jack" : true }, "id" : 4, "_id" : ObjectId("542038e4a3fbfcf81bace0d7") }
I suspect those "begin", "end", "created", "refDate" fields was inserted as String, not Date (or ISODate).
I'm wrong?
Thanks.
Upvotes: 2
Views: 2808
Reputation: 47956
You don't need to convert anything to BSON. Mongo handles this for you. Mongo only uses BSON to store it's data internally - you interface with it using JSON and your library code (in your case node-mongodb-native
).
With regard to the date objects - you can create them as normal JavaScript Date objects - again, mongo will handle these and convert them as needed during the insertion process.
Upvotes: 3