Consider, Mongoose schema:
var schema_obj = new Schema({
field1: String,
field2: String, .........
});
JSON document, like:
var json_obj = {
field1: 'value',
field2 : 'val2',
............
};
For saving I call method, like
var Model_obj = mongoose.model('model_name', schema_object);
var document_obj = new Model_obj(json_obj);
document_obj.save(function(err,data){/*some logic after save*/});
Now my question is: Why should I create a json_obj.
When I already have a schema object in my hand, which already has all the fields (field1, field2). If I just want to give values for those fields why should I create json by writing all the field name again?
If I have n number of fields this becomes a overhead to write all fields again. Is there any way to avoid this overhead?
Something like I get a empty JSON object out of my defined mongoose schema, then just proceed by assigning only values?
What kind of API are you looking for? You can set properties on a model instance and
saveit. But I’m not sure if I understand whyis easier than
In a web app, this becomes something like