Answer the question
In order to leave comments, you need to log in
What data to store in mongodb, in what data in mysql?
Hello! I am making a store, it has categories and I store their products in mysql with many to many relationships.
You can add an unlimited number of options and options to products, for example:
Option: Size; Options: m, l, s;
Option: Color; Options: blue, green;
I store options in mongodb so as not to create 3-4 more tables and then not to make large joins to collect options, options and products in mysql, in mongo the options look like this:
import mongoose, { Schema } from 'mongoose'
const ProductOptionSchema = new Schema({
productId: {
type: Number,
required: true,
},
name: {
type: String,
required: true,
},
options: {
type: [
{
name: {
type: String,
required: true,
},
value: {
type: String,
required: true,
},
},
],
required: true,
},
})
export default mongoose.model('productOption', ProductOptionSchema)
Answer the question
In order to leave comments, you need to log in
Why do you need Mongo in this case? Options can be joined quite normally, provided that the indexes are set correctly. After the first time, the request will be cached and will work pretty quickly. This is provided that you write a query using binds.
A good example of using mongo is logs from various sources. This is data without a strict structure.
But the data from the business logic fits perfectly into the SQL DBMS, because the entities are interconnected. Including your product properties divided into three tables (products themselves, property names, property values for products). It is also possible to have a table for links between allowed properties and product categories. But this is a simplified model; in a more complex one, you will have to separate the name of the product from the article.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question