1

I have this 2 schemas in mongoose:

The Booking schema

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var bookingSchema = new Schema({
bookingNO:          { type: Number, unique: true},
plateNO:      String,
startDate:    String,
bookedTime:   Number,
creator:      {type: Schema.Types.ObjectId, ref: 'User'}
});

var Booking = mongoose.model('Booking', bookingSchema);

The User schema:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new Schema({
username:   String,
password:   String,
balance:    Number,
bookings:   [{type: Schema.Types.ObjectId, ref: 'Booking'}]    

});

var User = mongoose.model('User', userSchema);

The problem with this design is:

Some Bookings were NOT created by a user - in which case the 'creator' field in Booking would be empty. Likewise, some Users do NOT necessarily contain a Booking - they may later on.

I was thinking about deleting the creator and bookings fields from the 2 Schemas, and using the { strict: false } option in Mongoose. Would this be the best option ?

If this is the case, I would have to add the 'creator' property to the Booking model, and 'bookings' property to the User, which would then get saved to the DB.

MOST importantly, due to the fact that I've removed the references from the Schema, how do I go about creating the reference in the case of using { strict: false } ?

Thanks in advance.

2 Answers 2

0

Unless a field has require:true flag, it can be left empty.

If you have a field which isn't defined in the Mongoose schema but is present in a document in MongoDB, you'll have to use doc.get('field') instead of just doc.field. Similarly for saving doc.set('field', value) and strict:false will be required otherwise it won't persist.

IMO you should have your schema inclusive rather than exclusive. Mongoose is just a wrapper for your data in MongoDB which at its heart is already schemaless.

2
  • laggingreflex, what do you mean by inclusive/exclusive Schema? Sep 11, 2015 at 21:37
  • I mean include as many fields as you think you might need, or you think will be present in the already saved documents, rather than excluding fields you think you might not need frequently or in future. They won't actually be saved to the database unless you set them. But if you leave them out, and you need to access/set some data which actually exist in the MongoDB document then you'll need to go the hard way of accessing/setting them with doc.get/set Sep 11, 2015 at 21:46
0

In your specific case, you can create a booking without specifying a 'creator', because its of type ObjectId Mongoose will simply create the document and will leave that field empty. The user.bookings is a different matter as it is an Array, in which case Mongoose will always default to an empty array, even if it was left undefined when creating the document. In this case, would it be that bad to just leave the empty array there? It still represents the data accurately, where the user simply has no bookings, but are still able to potentially have them. If you explicitly don't want bookings, then yes, you'll either have to deal with strict: false, or manually $unset/delete the field from the documents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.