node.js - mongodb insert() always successful -
i have written following mongoose schema collection. mongoose schema doesn't seem executing strict check @ data types of objects , items being inserted in document. forexample: if change
"activity_performed":true
to
"activity_performed":"adsfsadf"
the mongodb doesn't give error while inserting data. doing wrong , how can enforce mongoose , mongodb practice strict type checking while inserting data.
function initschema() { activityschema = mongoose.schema({ observationnumber : number, observation_data : [{ child_id : number, child_observation_data : [{ activity_performed: boolean, observation_date: string, teacher_id: number }] }] }); } function insertdata() { var activitymodelobject = new activitymodel(); activitymodelobject.observationnumber = 1593570; activitymodelobject.observation_data = {"child_id" : 236, child_observation_data:{"activity_performed":"adsfsadf", "observation_date": "16may2016", "teacher_id":789}}; activitymodelobject.save(function(err, savedobject) { if(err) { console.error("error in saving mongodb", err); return; } console.log("succesffully saveed data: ", savedobject); }); }
log:
successfully saved data: { observation_data: [ { child_observation_data: [object], _id: 57d07545b839132412da0755, child_id: 236 } ], _id: 57d07545b839132412da0754, observationnumber: 1593570, __v: 0 }
this should have failed! reason mongodb reports successful
because code not complete. guessing have not register schema mongoose. so, mongoose not aware there rule want enforce. notice need initialize new mongoose.schema()
activityschema = new mongoose.schema({ observationnumber : number, observation_data : [{ child_id : number, child_observation_data : [{ activity_performed: boolean, observation_date: string, teacher_id: number }] }] }); //registering schema activity_model var activity = mongoose.model("activity_model", activityschema); var activitymodelobject = new activity();
Comments
Post a Comment