How to insert date into mongo from JSON file

I have a file .json

that contains a date object. How can I make sure this date field is inserted as date datatype in mongo?

I need this to be done via node.js.

{
    "name": "Jeff Johnson",
    "email": "jeff@gmail.com",
    "phone": "5555555555",
    "date_added": "2014-01-22T14:56:59.301Z"
}

      

+3


source to share


3 answers


The core of this is basically that your JSON you are using does not "store" the "datatype", so the "date" is defined, and in fact it is simply represented as a "string". The other side, of course, is that MongoDB itself is actually "schematic", so there is no built-in information for the "database" that this field should be a date.

With this in mind, there are several approaches that range from your "current" implementation to "future" considerations.

However, you are parsing the data (and there are many parser implementations out there), then your "application logic" should "know" that this field is actually "date" and then process that translation itself. So, down to the document level:

var obj = JSON.parse(singleJSONDoc);
obj.date_added = new Date(obj.date_added);

      

This will be the basis of the code that you would run for every valid JSON document in the input, with the very fundamental assumption that the input can be read one line at a time. The same approach changes for the "thread analyzer" when you then load the main object for modification, or even some libraries may support "hooks" so that you can customize the parsed object.

As another approach, logic can be embedded into your applications in its own schema concept. This is how most ODM implementations work (one example is mongoose) where you define schema and data "types". The translations are then done for you based on your 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
})

      



Thus, "logic" simply moves from one place in your code to the "compartment" that controls the schema and "types". Conversions are done for you based on the implemented "type". For example Mongoose does Date

out of the box. There are other solutions, but this is the basic premise.

Finally, another way to approach this is in the JSON representation itself. MongoDB introduced a concept a while back known as "extended json syntax" , it was not "borrowed" from EJSON .

The general idea here is to "allow" the JSON parser, be it serialization or de-serialization, to handle the type conversions for you. So basically, while JavaScript (and other languages) have the concept of "types", JSON itself is just a "string" format. Therefore, there is special handling to "store" this type information in a "serialized" form so that it can be "de-serialized" with the "types" stored.

The format will look like this:

{
    "name": "Jeff Johnson",
    "email": "jeff@gmail.com",
    "phone": "5555555555",
    "date_added": { "$date": "2014-01-22T14:56:59.301Z" }
}

      

This is usually supported by parsers implementing the EJSON specification in other tools such as mongoimport

and mongoexport

as well as some of the built-in driver implementations (Java, C # to name two).

If you can work with your input in this format, you can simply use a parser and nothing else is needed to handle the transformation. The good thing is that you can use the same parser to "export" the information, so this is useful when passing JSON data to the client, where now the "client" can also be "aware" and handle the type conversion correctly.

So the short answer is: it won't work if you don't do some work. MongoDB does not support schema, which depends on your application. And smart conversions are not part of the main JSON. Use other libraries and code to handle conversions.

+1


source


You should use an object data modeling library like mongoose.js, which provides a rigorous modeling environment for your data. Example:



var pet = new Schema({
        animal: {
            type    : String,
            enum    :  ['cat', 'dog', 'snake', 'hamster'],
            require : true
        },
        age: {
            type    : Number,
            enum    : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            require : true
        },
        color: {
            type: String
        },
        modified: {
            type    : Date,
            default : Date.now
        }
});

      

0


source


Prepare your data before inserting it into the database by converting data types accordingly.

1: Split the JSON into an object 2: change the values ​​accordingly 2.1: For dates you can simply do:

some_object["date_added"] = new Date(some_object["date_added"]);

      

for numbers (integers) it would be better to store them as such also;)

0


source







All Articles