Mongodb架构设计

发布于 2024-10-07 07:47:05 字数 1179 浏览 0 评论 0原文

我很困惑,以下情况的最佳格式是什么:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-10-14 07:47:05

阅读 MongoDb 文档中的架构设计。这正是你的情况。

Read Schema Design from MongoDb docs. This is exactly your case.

提赋 2024-10-14 07:47:05

我会说不。除非您需要灵活地从单位查询(所有具有一定成本的项目),否则我不会。如果需要,您可以随时使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文