mongodb数据设计问题

发布于 2024-08-21 11:40:39 字数 1042 浏览 6 评论 0原文

我正在使用 mongo_mapper 尝试在 Rails 上使用 mongodb 来尝试我的第一个应用程序,并且我正在权衡 STI 模型上的选项,如下所示。

它工作得很好,我当然会以比我目前数不清的更多方式添加到此,我只是好奇如果使用嵌入式文档或类似的东西不会更好。

我希望我的模型尽可能多地共享 IE,因为它们都继承了某些属性,property/_form.html.erb 上的共享表单部分...除了它们自己独特的表单元素等之外。我知道这些视图会有所不同,但我还不确定控制器,因为我可以使用我假设的属性控制器来处理大多数事情?我确信随着我的进展,事情会变得更加复杂。

任何指示资源和/或智慧(省痛技巧)将不胜感激

property.rb

class Property
      include MongoMapper::Document
      
      key :name, String, :required => true
      key :_type, String, :required => true
      key :location_id, Integer, :required => true
      key :description, String
      key :phone, String
      key :address, String
      key :url, String
      key :lat, Numeric
      key :lng, Numeric
      key :user_id, Integer, :required => true
      timestamps!
    
    end

餐厅

class Restaurant < Property
  key :cuisine_types, Array, :required => true
  
end

酒吧

class Bar < Property
  key :beers_on_tap, Array
  
end

I'm trying my first application with mongodb on Rails using mongo_mapper and I'm weighing my options on an STI model like below.

It works fine, and I will of course add to this in more ways than I can currently count, I just curious if I wouldn't be better off with Embedded Documents or some such.

I'd like my models to share as much as possible, IE since they all inherit certain attributes, a shared form partial on property/_form.html.erb... in addition to their own unique form elements etc. I know the views will differ but I'm not sure on the controllers yet, as I could use property controller I assume for most things? And I'm sure it will get more complex as I go along.

Any pointers resources and/or wisdom (pain saving tips) would be greatly appreciated

property.rb

class Property
      include MongoMapper::Document
      
      key :name, String, :required => true
      key :_type, String, :required => true
      key :location_id, Integer, :required => true
      key :description, String
      key :phone, String
      key :address, String
      key :url, String
      key :lat, Numeric
      key :lng, Numeric
      key :user_id, Integer, :required => true
      timestamps!
    
    end

restaurant

class Restaurant < Property
  key :cuisine_types, Array, :required => true
  
end

bar

class Bar < Property
  key :beers_on_tap, Array
  
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

放手` 2024-08-28 11:40:39

不要害怕更多的模型,OO 的想法是能够将您的关注点切成小块,然后以需要处理的方式处理它们。

例如,您的 Property 模型似乎做了很多事情。为什么不将您所处理的地理信息拆分到嵌入式文档中(纬度、经度、地址等)?这样您的代码将保持更简单且更具可读性。

我自己使用这种 STI,我发现它使我的代码更简单、更可用。使用 Mongo 这样的数据库的好处之一是,您可以像这样执行非常复杂的 STI,并且仍然拥有可管理的数据集合。

关于你的 food_types 和 beers_on_tap 等,我认为这些都是很好的概念。拥有 Cuisine 和 Beer 模型也可能很有用,因此您的数据库保持更加规范化(这个概念在 Mongo 中很容易丢失)。例如:

class Bar < Property
  key :beer_ids, Array
  many :beers, :in => :beer_ids
end
class Beer
  include MongoMapper:Document
  key :name, String
end

Don't be afraid of more models, the idea of OO is to be able to cut up your concerns into tiny pieces and then treat each of them in the way they need to be treated.

For example, your Property model seems to be doing a whole lot. Why not split out the geo stuff you've got going on into an EmbeddedDocument (lat, lng, address, etc)? That way your code will remain simpler and more readable.

I use this sort of STI myself and I find it makes my code much simpler and more useable. One of the beauties of using a DB like Mongo is that you can do very complex STI like this and still have a manageable collection of data.

Regarding your cuisine_types and beers_on_tap etc, I think those are fine concepts. It might be useful to have Cuisine and Beer models too, so your database remains more normalized (a concept that is easy to lose in Mongo). e.g.:

class Bar < Property
  key :beer_ids, Array
  many :beers, :in => :beer_ids
end
class Beer
  include MongoMapper:Document
  key :name, String
end
微凉徒眸意 2024-08-28 11:40:39

您是否希望在同一查询中返回餐馆和酒吧?

如果不是,您可能需要重新考虑让它们从基类型派生。

默认情况下,Mongo_Mapper 会将餐厅和酒吧放在一个集合中。这可能会影响性能并使未来更难扩展。

查看一些 Mongo_Mapper 代码,看起来您可以使用 set_collection_name 动态设置它。

Do you expect to return both Restaurants and Bars in the same query?

If not, you might want to reconsider having them derive from a base type.

By default, Mongo_Mapper is going to put both Restaurants and Bars in a single collection. This could hamper performance and make things harder to scale in the future.

Looking through some of the Mongo_Mapper code, it looks like you might be able to set this on the fly with set_collection_name.

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