javascript - How to insert date into mongo from JSON file -
i have .json
file contains object date. how can make sure date field inserted "date" datatype in mongo?
i need done via node.js.
{ "name": "jeff johnson", "email": "jeff@gmail.com", "phone": "5555555555", "date_added": "2014-01-22t14:56:59.301z" }
the core of json using not "preserve" "data type" "date" determined , in fact represented "string". other side of course mongodb in fact "schemaless", there no inherent information available "database" field should date.
with in mind, there few approaches vary "current" implementation "future" considerations.
however parsing data ( , there many parser implementations out there ), "your" application logic "know" field in fact "date", , handle translation itself. down document level:
var obj = json.parse(singlejsondoc); obj.date_added = new date(obj.date_added);
that basics of code implement per each valid json document in input, on basic assumption input can read 1 line @ time. same approach varies "stream parser" fed basic object modify, or libraries might support "hooks" allow customize parsed object.
as approach, logic can built in applications own concept of "schema". how odm implementations work ( 1 example being mongoose ) define schema , "types" of data there. translations made based on implementation.
for example: ( mongoose style ):
var personschema = new schema({ "name": { "type": string }, "email": { "type": string }, "phone": { "type": string }, "date_added": { "type": date, "default": date.now } }); var person = mongoose.model( "person", personschema ); var raw = json.parse(singlejsondoc); var person = new person(raw); person.save(function(err,doc) { // doc inserted here })
so "logic" moved 1 place in code "compartment" manages schema , "types". conversions done based on "type" implemented. mongoose date
out of box example. other solutions exist, basic premise.
finally, way approach in json representation itself. mongodb introduced time ago concept known "extended json syntax", has notablty been "borrowed" in ejson project.
the general idea here "enable" json parser, whether serializing or de-serializing handle "type" conversions you. while javascript (and other languages) have concept of "types", json "string" format. there special handling here "preserve" type information in "serialized" form can "de-serialized" "types" preserved.
a format this:
{ "name": "jeff johnson", "email": "jeff@gmail.com", "phone": "5555555555", "date_added": { "$date": "2014-01-22t14:56:59.301z" } }
this supported parsers implementing ejson spec , in other tools mongoimport
, mongoexport
, of native driver implementations ( java, c#, name 2 ).
if can work input data format, can use parser , nothing else required handle conversion. thing here can use same parser "export" information, useful when passing json data client, "client" can "aware" , handle type conversion properly.
so short answer is, nothing going done without doing work. mongodb not hold schema, application. , smart "type" conversions not part of basic json. use other libraries , code handle conversions.
Comments
Post a Comment