search

Home  >  Q&A  >  body text

mongoose - 关于mongodb的建模,有人建议我 把 “商品”的“品牌名” 从ObjectId 转为 string,这样好吗?

他说这样更方便查询,是呀,傻子也知道这样便于查询。我质疑说这样在存储时会不好,但他说有shortId什么的,求解答!

天蓬老师天蓬老师2761 days ago649

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱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.

    reply
    0
  • Cancelreply