I'm building app which is using MongoDB. In my app each user could have many different car models (Porshe, ferrari etc).
I need to keep them all in one collection called - car. So in one collection i would like to store similar documents type. Why ? because these all models are cars and they all has from 50 till 90% the same fields.
question: am I right ? or i should have separated collection for each new car type (model) ?
Why I need it ? Because I have to show user some table with list of all his cars, and this table (GRID) has to be sorted and filtered as well.
filter by fields like number of doors, number of gears etc... it would be easier to rake these data from one collection.
Yes, using one collection is the way to go here. But name it cars instead.
Related
I'm making a Laravel short story sharing application, and I have a question about how I should structure something:
Let's say I have a table for stories, and each story is allowed to have up to 2 genres applied to it. Right now I'm storing these genres directly in each story row in columns 'genre_primary' and 'genre_secondary'. I initially did this so I didn't have to use a relation between the Story model and a Genre model. I thought it might be more efficient.
BUT! I've learned about using eager loading and the Laravel Debugbar to track queries, and am using it to greatly cut down on querying while grabbing stories' authors for the byline in the main feed (using with('author') in the Story model retrieves the author from the User table at the time of story retrieval, eliminating that n+1 problem of an additional author query per story displayed in the feed).
Like the author name, I also display the story's primary and secondary genres on the story's card in the feed. I'm now wondering if I should keep the genres embedded as two columns the story row, or extract a Genre model, relate the Story and Genre models, and eager load them like the author name?
I guess my concern is over efficiency when loading a larger timeline of recent stories or stories filtered by a genre? It seems like just selecting stories where genre_primary = 'Mystery' would be more efficient than eager loading a relation to a separate Genre model? Is it fine to denormalize in this case? Or am I just overthinking this...
It seems you're pre-optimizing.
Do whatever's simplest for you right now to see your idea through. At your current scale (which I assume is the "prototype phase"), the difference between joining a second table or having two extra columns on a single model is negligible.
The being said, creating a many-to-many relationship in Laravel is painfully easy and the added flexibility of separating your Generes may benefit you as you're fleshing out your concept.
I want to ask if its clean possible to change the mapping process.
For example, if i have an database, with products and offers, so the relation is 1 product n offers.
I want to get on fetch an product with multiple offers not an collection of offers, instead of i want to get an class named "AggregateOffers" which calculates the highest the lowest price and includes the offers collection.
Did anyone know an good/clean solution?
For an better understanding, schema.org defines the relation between an product an multiple offers, within an aggregateoffer, so i want to abstract them, to generate at the end an json-ld.
My actually design looks like schema, as you can see i have an 1-n relation from Product to Offer, what i actually want to get when i read from the DB, is an extended relation.
For example:
Product -> getOffers (should not return an collection of offers, instead it should return an "AggregateOffers" which hold the collection of offers).
The only idea, is to update the "getOffers" function and return die Aggregation class, but this acts not really good on me.
I am doing a database for a project and im stuck in a point.
Since every product can have multiple field of use, but even every materials can have multiple field of use, i come up with that solution.
THis is my database architecture.
http://i57.tinypic.com/2mhc03o.jpg
product are specifical for every material e.g. there can't be the same product for 2 material
material are leather, simil-leather, cloth, PVC
field of use are the field which that material can be used: sport, leisure, work
The problem is that material can be used in many field and many field can be used for a material, so it's N:M
Every product can be used in many field and many field can be used for a product so it's too N:M
For example, leather can be used in work, sport, cloth in work sport and office
product can be used in some or all field of application and vice versa.
1)WIth my architecture, to retrieve a material that can be used in a specific field of use i need to do 4 JOIN between all the table. Is it ok? or it's too long?
2)Also, when the user want to add a new category, to insert which field of use that category can have, i need to have a product already for that category.
3)when i want to fill a many to many relationship, i need to do it manually in the conjuction table (field_of_use_product) with some php codes right?
You need three joins for four tables that involved.
No, product may insert after all of the data at foreign tables have inserted.
Yes, it's a simple insert if you know the foreign keys.
Let me start with a simple example to show how my data is structured. There are four tables, Employee, Store, Department, and Employee_Department_Permissions.
Departments belong to Stores (for example, the Department table might contain a record for the dairy department of store 5). Employees are given the right to work in Departments through the Employee_Department_Permissions table, which contains the Employee id and Department id.
Let's say an employee can log in to my application and view a list of every store in the database. Next to each store, I want to print out how many departments they can work in at each store. I have an Employee model with a mapper that provides the fetchAll method to accomplish the first part.
But, where should I find out how many departments an employee can work in? In my model wrapper, I can call findDependentRows to do this. Or, I could do it in my controller with raw Zend_Db_Select calls. A third option I was considering would to be just add a column to the Employee table that holds this information, but then I'd need to update a second table when Employee_Department_Permission is modified.
Thank you in advance for any advice.
As a very general rule of thumb, I would suggest you try keep the controller as free as possible from fetching information for the views. This is a task best handled in the model.
Sure it's easy to just fetch from controller, I mean, since we are there processing a request, it would be so simple to just do a quick fetch and push that off to the view. This is where dicipline comes into play. As your application grows you will appreciate having the clean separation this methodology offers you if applied.
My 2 cents, happy coding to you friend.
I would like to have categories, and rankings for my content and users respectively, and I am not sure how to go about implementing this using CakePHP conventions?
Do I need to create a model?
That depends entirely on what these categories are supposed to do and not do. You could simply define a number of constants that you use for categorizing stuff. But, are categories...
subject to change? Do you want to add more eventually?
editable? May you want to change their names?
nested?
supposed to have more attributes than just their id? Names, descriptions?
If you answered Yes to any of the above, you'll want to store them as data in the database. This is independent of Cake, it's just sane data modeling. For Cake that means you'll need to create a model. The same goes for ratings.
So you'll have these tables:
users
hasMany ratings
categories
hasMany contents
contents
belongsTo categories
hasMany ratings
ratings
belongsTo users (polymorphic)
belongsTo contents (polymorphic)
You may want to separate user ratings and content ratings into two tables instead of using a combined polymorphic table (which just means that you have an extra column keeping track of whether a rating is for a user or for content).
i guess you are looking for something like this IF you dont want to use a model:
http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
one possible approach to use "enums" for things that maybe only have 1-5 states.
if you have more than 10 or you want to be able to dynamically modify them (label, active/inactive) you will need a separate table and model relation.