他说这样更方便查询,是呀,傻子也知道这样便于查询。我质疑说这样在存储时会不好,但他说有shortId什么的,求解答!
给我你的怀抱2017-04-25 09:04:36
This requires detailed analysis of specific businesses.
If the brand name is set to ObjectId
的话,就说明有另外一个 collection
, the brand is stored. This is a typical relational database method. For mongodb, each time you want to obtain the brand name of a product, you need one more read operation.
If set to String
, all product information and brand name can be retrieved in one operation, but it is very troublesome to modify the brand. If you also need to obtain some information about the brand in one request, such as the country where the brand is located, you still have to do it again. When doing a query, the scalability is not good enough.
If set to Object
For example:
javascript
{ item:"", price:100, brand:{ name: "micorsoft", country: "US" } }
This can solve the scalability problem, and the query speed is also very fast. If the product brand information is not updated very often, then I recommend this. Although the degree of data redundancy is increased, the query speed is improved. This is an anti-paradigm.
If the brand information is updated frequently, it is still good to use ObjectId
. Although it requires one more query, it improves reliability.