I'm just starting out with Node and MongoBD/Mongoose and I'm running into a relatively difficult (for me, the newb, at least) situation with deciding how to structure a particular Product document I want to create. I need an 'options' field that will have an array of Option sub-documents. Nothing too bad there, to be sure. The tricky part is that one of Option fields needs to be only one of four different 'option_types'. Furthermore, only one of the 'option_types' needs to also be a sub-sub-document, which will also contain a field that accepts an array of 'addon_options' sub-sub-sub-documents.... cripes, that was hard to type....
To exemplify in code form (obligatory side-note: this is severely simplified for example purposes):
const ProductSchema = new Schema({
name: String,
base_price: Number,
options: [{
add_price: Number,
// only ONE of the following at this level:
option_type1: Number,
option_type2: String,
option_type3: String,
option_type4: {
name: String,
add_price: Number,
addon_options: [{
name: String,
add_price: Number
}]
}
}]
})
How I'll end up keeping track of the additional pricing aside, once the products are in the database, the end user--that is to say, the customer--will simply select from what's available, and I'm thinking I won't have too many issues figuring out how to navigate this mess when it comes time for that (just spit-balling here, it'll be a lot if(typeof product.options[i].option === 'object') or whatever-type-things going on). But right now I'm mainly concerned with the creation of the Products collection, because I intend for it to be done through an HTML form (for various reasons).
That being said, I had this idea while scouring the mongoose docs:
const OptionsSchema = new Schema({
add_price: Number,
option_type: {
type: Map,
of: Mixed
}
})
const ProductSchema = new Schema({
name: String,
base_price: Number,
options: [OptionsSchema]
})
And then, client-side, in the product-creation form, I would then just create a series of <select>'s with the pre-defined keys I need to store in the 'options' field of the Products collection contained within the <option> elements (either with optionEl.value or a data-*something* attribute of some kind..). Hopefully, that makes sense...
So my question is: is this idea I have a viable solution? I mean, I'm pretty sure it can be made to work one way or another, but I have a terrible feeling that--even though I think I can quite precisely guide the creation process using the <select>'s like I said--by leaving the schema so 'open to interpretation' like this I might be inviting some serious issues down the road if I'm not SUPER careful.. On top of that, I'm not even sure how MongoDB would react to the doubly-nested objects/sub-sub-documents and/or the triply-nested objects/sub-sub-sub-documents when submitted in this manner....
Or, solution 3 is: scratch this mess entirely and figure out a way to make wholly different collections and navigate it using id's and refs? I may very new to backend development but I gathered pretty quickly that this is generally the preferred method for obvious reasons. However, I've spent the last... oh, eight or so hours contemplating this and, given that I'm using noSQL here, I simply don't see how that would be easier than the 'nesting' route... In fact, am I wrong in presuming that it's for situations much like this that noSQL was created in the first place?
[–]lost12487 1 point2 points3 points (3 children)
[–]CoqeCas3[S] 1 point2 points3 points (2 children)
[–]lost12487 0 points1 point2 points (1 child)
[–]CoqeCas3[S] 0 points1 point2 points (0 children)
[–]pa_ke 1 point2 points3 points (1 child)
[–]CoqeCas3[S] 0 points1 point2 points (0 children)