深入了解 MongoDB、mongomapper 和连接

发布于 2024-09-02 19:31:43 字数 594 浏览 8 评论 0原文

我是 MongoDB 新手,并且已经使用 RDBMS 多年。

不管怎样,假设我有以下集合:

Realtors
 many :bookmarks
 key :name


Houses
 key :address, String
 key :bathrooms, Integer


Properties
 key :address, String
 key :landtype, String


Bookmark
 key :notes

我希望房地产经纪人能够为房屋和/或房产添加书签。请注意,房屋和房产是独立的,不知道房地产经纪人或书签。我希望书签有点像 MySQL 中的“连接表”。

房屋/财产来自不同的来源,因此无法修改。

我希望能够在 Rails 中执行此操作:

r = Realtor.first r.bookmarks 会给我:

House1 房子2 地产ABC 物业OO1 等等......

将会有数千栋房屋和财产。

我意识到这就是 RDBMS 的用途。但我使用 MongoDB 有几个原因,所以我想让它发挥作用。

任何关于如何做这样的事情的建议将不胜感激。

谢谢!

I'm new to MongoDB and I've used RDBMS for years.

Anyway, let's say I have the following collections:

Realtors
 many :bookmarks
 key :name


Houses
 key :address, String
 key :bathrooms, Integer


Properties
 key :address, String
 key :landtype, String


Bookmark
 key :notes

I want a Realtor to be able to bookmark a House and/or a Property. Notice that Houses and Properties are stand-alone and have no idea about Realtors or Bookmarks. I want the Bookmark to be sort of like a "join table" in MySQL.

The Houses/Properties come from a different source so they can't be modified.

I would like to be able to do this in Rails:

r = Realtor.first
r.bookmarks would give me:

House1
House2
PropertyABC
PropertyOO1
etc...

There will be thousands of Houses and Properties.

I realize that this is what RDBMS were made for. But there are several reasons why I am using MongoDB so I would like to make this work.

Any suggestions on how to do something like this would be appreciated.

Thanks!

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

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

发布评论

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

评论(1

简单爱 2024-09-09 19:31:43

好的,首先要做的事情。您已经将数据结构化为 RDBMS。您甚至创建了一个“连接表”,就好像这样的东西在 Mongo 中很有用一样。

对您问题的简短回答是,您可能需要重新定义“第一个”来加载给定的“书签”。带有 $in 子句的“服务器端”或带有大 for 循环的“客户端”。

关于数据的两个大问题:

  1. 如果书签完全属于房地产经纪人,为什么它们在他们自己的集合中?
  2. 如果房地产经纪人可以为房屋和房产添加书签,那么为什么它们位于不同的集合中?这不是不必要的复杂化吗?如果您想要像 Realtor.first 这样的书签,为什么要把它们放在不同的集合中?

Realtors 集合可能应该由如下所示的项目组成:

{"name":"John", "bookmarks": [ 
   {"h":"House1","notes":[{"Nice location","High Ask"}] },
   {"p":"PropertyABC","notes":[{"Haunted"}] }
] }

请注意我如何区分房屋 ID 和财产 ID 的“h”和“p”?如果您接受我的下一个建议,您甚至不需要那个。

更进一步,您可能希望房屋和房产位于同一个集合中,例如“位置”。在“Locations”集合中,您只需填充所有 Houses 和 Properties 并用“type”:“house”或“type”:“property”标记它们。然后您将在“类型”字段上建立索引。

为什么?因为现在当您编写“第一个”方法时,您的查询非常简单。您要做的就是循环遍历“书签”并从“Locations”集合中获取适当的键(“House1”、“PropertyABC”)。分页很直接,查询 10 项然后返回。

我知道在某种程度上这似乎有点蹩脚。“为什么我要编写一个 for 循环来抓取数据?我在 15 年前就试图停止这样做!”但是 Mongo 是一个“面向文档的” " 存储,因此它针对加载单个文档进行了优化。您正在尝试加载一堆文档,因此您必须跳过这个小环。

幸运的是,这并不全是坏事。 Mongo 在加载单个文档方面确实非常快。运行一次查询来获取 10 个项目仍然非常快。

OK, first things first. You've structured your data as if this were an RDBMS. You've even run off and created a "join table" as if such a thing were useful in Mongo.

The short answer to your question is that you're probably going to have re-define "first" to load the given "Bookmarks". Either "server-side" with an $in clause or "client-side" with a big for loop.

So two Big Questions about the data:

  1. If Bookmarks completely belong to a Realtor, why are they in their own collection?
  2. If Realtors can Bookmark Houses and Property, then why are these in different collections? Isn't this needless complication? If you want something like Realtor.first on bookmarks why put them in different collections?

The Realtors collection should probably be composed of items that look like this:

{"name":"John", "bookmarks": [ 
   {"h":"House1","notes":[{"Nice location","High Ask"}] },
   {"p":"PropertyABC","notes":[{"Haunted"}] }
] }

Note how I've differentiated "h" and "p" for ID of the house and ID of the property? If you take my next suggestion you won't need even that.

Taking this one step further, you probably want Houses and Properties in the same collection, say "Locations". In the "Locations" collection, you're just going to stuff all Houses and Properties and mark them with "type":"house" or "type":"property". Then you'll index on the "type" field.

Why? Because now when you write the "first" method, your query is pretty easy. All you do is loop through "bookmarks" and grab the appropriate key ("House1", "PropertyABC") from the "Locations" collection. Paging is straight forward, you query for 10 items and then return.

I know that at some level it seems kind of lame."Why am I writing a for loop to grab data? I tried to stop doing that 15 years ago!" But Mongo is a "document-oriented" store, so it's optimized for loading individual documents. You're trying to load a bunch of documents, so you have to jump through this little hoop.

Fortunately, it's not all bad. Mongo is really fast at loading individual docs. Running a query to fetch 10 items at once is still going to be very quick.

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