Mongoose: Repeated object in schema - mongodb

I'm defining my schema in mongoose and I have an array of book objects, and then an "active book". Now setting it up wasn't an issue, but this seems like unnecessary repetition to define the exact same book object in two different parts of the schema.
var BookSchema = new Schema({
activeBook: {
id: String,
title: String,
author: String,
pages: Number
},
books: [{
id: String,
title: String,
author: String,
pages: Number
}]
});
Is there a cleaner way of writing this so I don't have to write out the same object everywhere I use it?

A cleaner way would be to create a subdocument, a document with a schema of its own which are elements of a parent's document array. So in your example above you can define the child/parent schema as follows:
var ChildSchema = new Schema({
id: String,
title: String,
author: String,
pages: Number
});
var ParentSchema = new Schema({
activeBook: ChildSchema,
books: [ChildSchema]
});

Related

Reusing parts of Mongoose schema such as common fields between models

I have a schema which shares multiple fields between my model. Here are a simple representation of the schemas.
const productSchema = new mongoose.Schema({
productName: String,
productId: String,
productAltCode: String,
description: String,
}
const cartSchema: new mongoose.Schema({
productId: String,
productAltCode: String,
...
}
Here I have 2 fields which are the same in both models, productId and productAltCode. Is there a way to not duplicate in both schemas? I have this type of scenario with other objects in my model and just think there may a way to not duplicate the fields

Initial POST to MongoDB creates an empty object within an array I haven't added anything to

Using the MERN stack I am able to add a document (a Property Schema in this case) via Mongoose. The issue is one of the Property Keys (Rooms in this case) is an Array of Objects. When I initially create the Property I don't send any data regarding the Rooms but a blank Object is created, albeit with a MongoDB _id?
I thought Mongoose prevented creating blank Objects / Arrays if no data was sent or am I confusing matters? Why is it happening? And is there a way to prevent this?
Just to be clear when I initially create the Property I'm sending no information and I don't even reference the rooms array in the data sent from axios.
Here is my Schema:
const propertySchema = new Schema({
propertyId: String,
propertyName: String,
rooms: [
rId: String,
type: String,
class: String,
view: String,
price: String
]
})
Arrays implicitly have a default value of [] (empty array).
But you can prevent it by giving a default: undefined option like this:
const propertySchema = new Schema({
propertyId: String,
propertyName: String,
rooms: {
type: [
new Schema({
rId: String,
type: String,
class: String,
view: String,
price: String,
}),
],
default: undefined,
},
});
Docs (in the Arrays section)
What I realised was I had two controllers, postPropertyController and patchPropertyController. As described in the question when I post the property for the first time I don't include anything in the req.body about the rooms. However, in the postPropertyController I was still doing this...
const propertySchema = new Schema({
propertyId: String,
propertyName: String,
rooms: [
rId: String,
type: String,
class: String,
view: String,
price: String
]
})
What I needed to do to clear the empty object in the rooms array was this...
const propertySchema = new Schema({
propertyId: String,
propertyName: String,
rooms: []
})
Later in the application flow I used a patch method and the patchPropertyController to update the rooms array.
Shout out to #suleymanSah for suggesting something that made me take another look at the code side by side.

Mongoose - Divide subdocuments using discriminators

If I have a model Attachment, which can be divided into 4 types: Link, YoutubeVideo, GoogleDriveFile, and GoogleDriveFolder, how can I use Mongoose to discriminate Attachment into these types, and allow them to be subdocuments in another schema; Post?
I've created the base Attachment model, and divided it into separate models using discriminators:
var AttachmentSchema = new Schema({
id: {type: String, required: true},
title: {type: String, required: true}
});
var Attachment = mongoose.model('Material', AttachmentSchema);
module.exports = {
DriveFile: Attachment.discriminator('GoogleDriveFile', new mongoose.Schema()),
DriveFolder: Attachment.discriminator('GoogleDriveFolder', new mongoose.Schema()),
Link: Attachment.discriminator('Link', new mongoose.Schema()),
YoutubeVideo: Attachment.discriminator('YoutubeVideo', new mongoose.Schema())
};
Now, in the Post schema, there should be an array of attachments, with varying types:
var Attachment = require('./attachment');
var PostSchema = new Schema(
text:{type: String},
attachments: [Material] // Could be Material.Link, Material.YoutubeVideo, etc
});
When I do this, I get an error saying "Undefined type Model at GoogleDriveFile. Did you try nesting Schemas? You can only nest using refs or arrays."
I don't know what this error means, and I can't find any docs explaining how to do this. Help?
Try doing the following:
var AttachmentSchema = new Schema({
id: {type: String, required: true},
title: {type: String, required: true}
});
var PostSchema = new Schema({
text: { type: String },
attachments: [ AttachmentSchema ] // Could be Material.Link, Material.YoutubeVideo, etc
});
var attachmentArray = PostSchema.path('attachments');
module.exports = {
Post: mongoose.model('Post', PostSchema),
DriveFile: attachmentArray.discriminator('GoogleDriveFile', new mongoose.Schema({})),
DriveFolder: attachmentArray.discriminator('GoogleDriveFolder', new mongoose.Schema({})),
Link: attachmentArray.discriminator('Link', new mongoose.Schema({})),
YoutubeVideo: attachmentArray.discriminator('YoutubeVideo', new mongoose.Schema({}))
};
The key is to NOT use a mongoose Model use the schema.path of the parent document schema as the base for your discriminators.
search for the term docArray on this link: Mongoose Discriminator documentation

Mongoose variable schema reference

I'm looking for the best way to have a parent model that references variable child models. Is there a way to set the reference upon saving the parent model, then have it populate the child automatically? If not, what is the best way of achieving a parent model that has variable sub-fields.
var ChildSchema1 = new Schema({
field1: String,
field2: String
});
var ChildSchema2 = new Schema ({
field3: Number,
field4: String
});
var ParentSchema = new Schema({
name: {type: String, required: true},
child_ref: {type: ObjectId, ref: ChildSchema1 OR ChildSchema2}
});
I had a similar problem, where I needed to do schema inheritance. It seems like this github project will work for us both:
https://github.com/briankircho/mongoose-schema-extend

tuples (arrays) as attributes in mongoose.js

MongoDB, and mongoose.js specifically, allows tuples as attributes. For instance, the MongoDB documentation has this example where the attribute comments is itself an array of objects with the attributes [{body: String, date: Date}]. Yay!
var blogSchema = new Schema({
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
})
Now when i persist that to MongoDB, not only does each instance of blogSchema get its own value for _id (e.g. 502efea0db22660000000002) but each individual value of comment gets its own _id field.
For the most part I don't care, but in my app the analog to comments may have thousands of values. Each of which gets its own huge value of _id.
Can I prevent that? I'll never need to refer to them individually. Or should I learn to stop worrying and love the unique identifier? I grew up programming the Vic20 and TRS80 as a kid, so may be overly paranoid about wasting memory/storage.
The _id can be disabled by setting the noId schema option to true. To pass the option you need to pass an instance of schema instead of using the object literal:
// instead of this...
comments: [{ body: String, date: Date }]
// do this...
var commentSchema = new Schema({ body: String, date: Date }, { noId: true });
var blogSchema = new Schema({
..
comments: [commentSchema]
})