Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an efficient MongoDB schema design for a multilingual e-commerce site?

I'm designing a multilingual e-commerce site. Products have different properties. Some properties are different for each language (like color), other properties are the same for all languages (like SKU). Properties are not predefined, for example cars have other properties than espresso machines.

I'd like to design the database schema such that:

  1. Searching and representing all products from category x in language y is fast
  2. The amount of duplicate data is low
  3. I don't want to use files with translations

I'm thinking about using a schema like this:

{
 _id: ObjectID("5dd87bd8d77d094c458d2a33"),

 multi-lingual-properties: ["name", "description"],

 name: { en: "Super fast car",
         nl: "Hele snelle auto"},

 description: { en: "Buy this car",
                nl: "Koop deze auto"},

 price: 20000,

 sku: "SFC106X",

 categories: [ObjectID("4bd87bd8277d094c458d2a43")]
}

Is there a better alternative to this schema? What problems will I encounter when I use this schema?

like image 330
i.amniels Avatar asked Sep 23 '11 12:09

i.amniels


People also ask

Is MongoDB good for ecommerce?

MongoDB is a document database for easy creation and scaling. Documents are created and stored in BSON (Binary JSON) format. Thanks to the use of JSON, it's very easy to convert the queries and results into a format that can “understand” the frontend code of the e-commerce application.

What the most important consideration while designing the schema for MongoDB?

General Rules for MongoDB Schema Design: Rule 1: Favor embedding unless there is a compelling reason not to. Rule 2: Needing to access an object on its own is a compelling reason not to embed it. Rule 3: Avoid joins and lookups if possible, but don't be afraid if they can provide a better schema design.

Which of the following things while designing the schema in MongoDB?

Considerations while designing Schema in MongoDBCombine objects into one document if you will use them together. Otherwise separate them (but make sure there should not be need of joins). Duplicate the data (but limited) because disk space is cheap as compare to compute time.

What is a MongoDB schema?

What is a Schema? A schema is a JSON object that defines the the structure and contents of your data. You can use Atlas App Services' BSON schemas, which extend the JSON Schema standard, to define your application's data model and validate documents whenever they're created, changed, or deleted.


1 Answers

Later than I expected, but here's what we're implementing now...

Background: Our system is supposed to be able to import product inventory from multiple ecommerce sites, so flexibility & i18n are important.

EAV model:

db.products.find()

{ "_id" : ObjectId("4e9bfb4b4403871c0f080000"), 
"name" : "some internal name", 
"sku" : "10001", 
"company" : { /* reference to another collection */ }, "price" : 99.99,
"attributes": { 
  "description": { "en": "english desc", "de": "deutsche Beschreibung" },
  "another_field": { "en": "something", "de": "etwas"}, 
  "non_i18n_field": { "*": xxx } 
 }
}

We also need metadata for attributes, which includes admin editing tips (what input forms to use) and i18n for names of the attributes. Example:

db.eav.attributes.find()

{ "_id" : ObjectId("127312318"), 
"code" : "description", 
"labels" : { "en" : "Description", "de": "Beschreibung" }, 
"type" : "text-long", 
"options" : [], 
"constraints" : [ ]
}

The idea is that attribute metadata will be quite large and won't get copied. Most of the time operations will be done using values of the (dynamic) attributes. If attribute metadata is necessary to display UI etc. it can be loaded & cached separately, and referenced by the attribute code.

So by default everything that's an attribute is i18n-enabled.

Queries for i18n-enabled-attributes are simple:

db.products.find({ attributes.description.en: "searched val" })

Non translated attributes may be a hassle though since they'd need special treatment in queries:

attributes.description.*

Not sure what we'll do with those attributes yet. E.g. numeric values don't require translation. Any thoughts on that are welcome.

We're still not using this structure though, so these are really pre-implementation ideas. I'm expecting more issues to surface while we start using this in practice, i.e. doing UI for CRUD operations etc.

like image 144
Jakub P. Avatar answered Oct 25 '22 15:10

Jakub P.