I wrote this very nice model/schema because.. someone told me this was the way to do it:
var Schema = mongoose.Schema;
var bookingSchema = new Schema({
_id: String, // Unique booking ID
property: String, // Property ID --> property.._id
checkin: Number, // Check in Date
checkout: Number, // Check out Date
priceNight: Number, // Price pr night in THB
priceReservation: Number, // Reservation Fee in THB
priceSecurity: Number, // Safety Deposit in THB
arrival: String, // When will tenant arrive
departure: String // When will tenant depart
});
module.exports = mongoose.model('bookingModel', bookingSchema, 'booking');
I then thought that this symbolizes the structure of your mongodb table/collection, but it seems to me it makes absolutely no difference weather I write a nice correct model or just make one field called whatever:string
.
So whats the point of making nice models if mongodb dosnt care about your nice structure?
Now I would like to create a new booking so I would of cause assume I can use my nice model to put in data, and then create the booking using my nice model.
var bookingModel = require('../models/bookingModel');
var bookingRecord = mongoose.model('bookingModel');
console.log("bookingRecord: " + bookingRecord);
console.log("bookingRecord: " + JSON.stringify(bookingRecord, null, 4));
bookingRecord.property = "WAT-606"
console.log("bookingRecord: " + bookingRecord);
console.log("bookingRecord: " + JSON.stringify(bookingRecord, null, 4));
But all I get here is the following output:
bookingRecord: function model(doc, fields, skipId) {
if (!(this instanceof model)) {
return new model(doc, fields, skipId);
}
Model.call(this, doc, fields, skipId);
}
bookingRecord: undefined
bookingRecord: function model(doc, fields, skipId) {
if (!(this instanceof model)) {
return new model(doc, fields, skipId);
}
Model.call(this, doc, fields, skipId);
}
bookingRecord: undefined
So once again, whats the point of writing these nice models?
I read a lot of docs but I feel none of them explains what these models and schema are good for when mongodb just ignores them anyway.