Doctrine ODM and schema-less design

后端 未结 2 1425
谎友^
谎友^ 2021-02-06 14:03

Continuing on from my question about EAV, I\'m considering using MongoDB to store product attributes.

I will store the catalogue part of this app - Categories, Products

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 14:04

    Even though nothing enforces this, it is a good practice to have a basic schema for a collection. Almost all ODM's allow you to add fields not specified in the class. Assuming the application allows it, you can also leave out field values.

    The real advantage of a schemaless datastore, however, is not so much that your top level fields might vary form document to document, but that those fields can be complex data structures. Every product may have and attributes field that is an array, but the content of that array can be arbitrarily long or short, and can contain hashes with various structures. Your ODM should add an Object layer on top of these hashes if you ask it to.

    The last advantage is in upgrading the schema. In SQL adding or removing a field is a monolithic operation that is also time consuming. With a little planning, you can add or remove fields from documents as they are accessed. You simply need code to deal with the out of date schema.

提交回复
热议问题