1

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.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53
  • 1
    That's the "model" of mongoose and not the "instance" of the data. You create an "instance" just like the [`JavaScript.prototypes`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype) which are actually returning from your `console.log` calls are telling you how to do. `var booking = new bookingModel()` and preferably with arguments. Once you have the instance, you can set properties on that instance. – Neil Lunn Jun 21 '17 at 23:06
  • Also. Nothing to do with MongoDB at all. This is purely mongoose and purely JavaScript. – Neil Lunn Jun 21 '17 at 23:08
  • @NeilLunn thanks for the explanation. So when creating a new booking, would you create a new `var bookingRecord = {}` and then fill in all the fields, and if you did that, can you then just insert it into the booking table? I am still having a hard time understanding why I write these models (other than they are good documentation of cause hehe) – torbenrudgaard Jun 21 '17 at 23:19
  • 1
    No. Please read the documentation. It ( as I have been ) are **Very clear** on this. `module.exports = mongoose.model('bookingModel', bookingSchema, 'booking')` Is a MODEL. `var BookingModel = require('../models/bookingModel')`; Grabs a scope reference to the MODEL. `var booking = new BookingModel({ property: "WAT-606" })` creates an INSTANCE of the model which you can set properties on, `.save()` etc. You are working with the wrong thing. Create an INSTANCE. The INSTANCE is what you MODIFY. – Neil Lunn Jun 21 '17 at 23:24
  • I Hope the big words highlight the "keywords" here. You are doing it all wrong. Use "Instances". Think of a "Model" as being a "Class". – Neil Lunn Jun 21 '17 at 23:25
  • Ahhh okie the "new" is what creates the instance, I totally didn't see that. So I only need to require the model in order to set a "new" instance of it, in which I can put my data. So why have I been doing `booking = mongoose.model('bookingModel');` ? What exactly does that one do? (I learned from code examples and its not all that I understand yet, but im getting there :-). – torbenrudgaard Jun 21 '17 at 23:31
  • And thanks for the explanation and yes, the big words do highlight it much more clearly, lights up in my brain :-D Thanks to you im starting to actually get this! – torbenrudgaard Jun 21 '17 at 23:33

0 Answers0