Mongodb模式设计 [英] Mongodb schema design

查看:120
本文介绍了Mongodb模式设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑,以下情况是最好的格式:

I'm confused, what is the best format for the following case:

Name: Pretty nice hot dog
Stock: 10
Weight: 0.1 grams
Price: 2 dollars

Name: An ordinary dumbbell
Stock: 5
Weight: 4 kilograms
Price: 667.98 yens

This:

db.item.save ({"_id" : 1, "name" : "Pretty nice hot dog", "stock" : 10, "weight" : {"value" : 0.1, "unit" : "gram"}, "price" : {"value" : 2, "unit" : "dollar"}})
db.item.save ({"_id" : 2, "name" : "An ordinary dumbbell", "stock" : 5, "weight" : {"value" : 4, "unit" : "kilogram"}, "price" : {"value" : 667.98, "unit" : "yen"}})

或者这个:

db.unit.save ({"_id" : 1, "name" : "dollar"})
db.unit.save ({"_id" : 2, "name" : "yen"})
db.unit.save ({"_id" : 4, "name" : "gram"})
db.unit.save ({"_id" : 5, "name" : "kilogram"})

db.item.save ({"_id" : 1, "name" : "Pretty nice hot dog", "stock" : 10, "weight" : {"value" : 0.1, "unit" : [new DBRef ("unit", 4)]}, "price" : [new DBRef ("unit", 1)]})
db.item.save ({"_id" : 2, "name" : "An ordinary dumbbell", "stock" : 5, "weight" : {"value" : 4, "unit" : [new DBRef ("unit", 5)]}, "price" : [new DBRef ("unit", 2)]})

字段单位的值是不可变的,我不知道我是否应该把它放在一个单独的集合中。

The values of the field "unit" is immutable, I don't know if I should put it in a separate collection.

谢谢

推荐答案

我会说不。除非您需要从单位(所有项目以一定的成本)查询的灵活性,否则我不会。如果你需要,你可以随时使用map reduce来获得一定成本的所有项目。

I'd say no. Unless you need the flexibility to query from the units (all items with a certain cost), I wouldn't. And if you need to, you can always use map reduce to get all items of a certain cost later on.

这篇关于Mongodb模式设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆