Mongodb架构设计
我很困惑,以下情况的最佳格式是什么:
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
这个:
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)]})
字段“unit”的值是不可变的,我不知道是否应该将其放在单独的集合中。
谢谢
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"}})
Or this:
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.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读 MongoDb 文档中的架构设计。这正是你的情况。
Read Schema Design from MongoDb docs. This is exactly your case.
我会说不。除非您需要灵活地从单位查询(所有具有一定成本的项目),否则我不会。如果需要,您可以随时使用 MapReduce 来获取特定成本的所有项目。
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.