My application is based on Node.js and MongoDB using rest api.
My input data is in the form
var doc={"name":"ABX", duedate : new Date() }
I'm stringifying it before passing it to the server
/rest/update?doc=JSON.stringify(doc)
and converting it back to JSON on server side as :
var doc = JSON.parse(doc)
Now the issue is that it stringifies the data as :
var doc = {"name":"ABX","duedate":"2014-04-30T18:30:00.000Z"}
If same value send to mongo, then it will save duedate as string. So need to do something like doc.duedate = new Date(doc.duedate). But I do not have any field definition, So how I know some field need to be converted to date and some not.
Similar is case when working with _id field of MongoDB.
My client side generates _id for document in 2 ways :
1 : _id:"student1" -> String type that is user specified
2: _id:535f7d05e1b08ddc13dfbaf0 -> ObjectId type generated by mongoDB.
For second type _id, when it is passed from client side it will be a string not a objectid. For both type of doc, user will send query something like /students/find?query={_id:"student1"} OR {_id:"535f7d05e1b08ddc13dfbaf0"} . Now at server second query must be modified as {_id: ObejctId("535f7d05e1b08ddc13dfbaf0")}.
I don't know how to distinguish between two.
Major issue I am facing is the datatype lost between sending doc from client side to server side in JSON
Now I'm wrapping it up in string form on client side and can convert it back on server side.
Since mongoDB isn't type specific i don't know what value i should convert on client side and what not.
Both user-defined and Self-generated are received on server side as String-type which makes re-converting a tough job.
My requirement is that i don't want conversion on server side.
- Client can send either Object or String and it should be stored as is on backend.
Is this possible in any way ?