我很困惑,以下情况的最佳格式是什么:
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”的值是不可变的,我不知道是否应该将其放在单独的集合中。
谢谢
最佳答案
我会说不。除非你需要从单位查询的灵活性(具有一定成本的所有项目),否则我不会。如果你需要,以后可以始终使用map reduce获取具有一定成本的所有项目。
关于mongodb - Mongodb架构设计,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4444911/