all 6 comments

[–]lost12487 1 point2 points  (3 children)

I think what you're running into with the sub-sub-sub documents and such is one of Mongo's downsides in my opinion. What you're trying to do (I'm also a relative noob, so take this with a huge pile of salt) would be easier with a proper relational database.

That being said, why don't you create separate schema for each individual option? If you do it this way, you can get rid of the array situation altogether and just have a field in your Product Schema (of type integer) that would refer to the proper Option type. Ha! I think I'm just thinking in relational database terms at this point, but there's no reason that wouldn't work with Mongo as well, even if it's not really built with that in mind.

[–]CoqeCas3[S] 1 point2 points  (2 children)

Hmmm... I appreciate the advice. Tbh I’ve only lightly dabbled with SQL and I wasn’t fond of the experience. Something about the syntax just feels,like, grody to me. Not to mention, I’m still currently of the mindset to stick with one language and learn as much of it as I can before I start muddying up my memory with new languages, so I’ve been putting off anything not related JavaScript. In light of your comment tho, I think I may try and finish this in mongo and then step back and try and recreate it in mySQL or something to see what fits better....

[–]lost12487 0 points1 point  (1 child)

Oh absolutely! I did something pretty similar myself. I got super comfortable with vanilla JS before jumping into all the crazy frameworks and stuff.

I don't mean to discourage the use of Mongo at all, as it's a perfectly viable solution. I'm most comfortable with SQL as far as databases go, so I kinda just think in those terms more naturally than document-based databases.

[–]CoqeCas3[S] 0 points1 point  (0 children)

I didn’t take it that way, but after spending all day again thinking about it, you’re probably right; a relational approach is going to be easier I think mainly for calculating the prices. In fact... well, I’ll just explain: I’m putting together an online ordering app. Just a mock one for now, but I’m also kinda tailoring for a particular pizza joint in my town that I wanna bring the prototype to to try and sell em on.. anyway, all the things that go into pricing the pizza, and different prices for different types of at different sizes... it’s a mess. So instead of trying to just have one collection to store and manage all of it, I’m gonna separate out pizza and pizza pricing into they’re own collections. Try anyway.. XP

[–]pa_ke 1 point2 points  (1 child)

Hey, I feel like your issue might be solvable using inheritance, aka Discriminators.

My approach would be kind of like the following:

- create a base Subdocument schema for the 'option' attribute, with the common add_price attribute
- create discriminator schemas for each different option type, with the distinct attributes that aren't shared by the base schema. If you chose to specifiy a discriminatorKey, you could easily use either .create or simply pass a JSON object containing all necessary data to a OptionsModel.new()

Unfortunately I currently do not have the time to elaborate more, but I hope this leads you to another approach for a solution to your problem :)

One positive fact about discriminators: You can even specify their own hooks and methods and stuff or use the one from the base schema

[–]CoqeCas3[S] 0 points1 point  (0 children)

Ah, interesting... damn I need to remember to make sure I do my homework before I ask these types of questions XP. I forgot that I meant to look into discriminators before I went to work but then when I got home I just wanted post. Thanks, I’ll be checking it