MongoDB/MongoID多键特殊查询

发布于 2024-11-03 11:12:27 字数 562 浏览 1 评论 0原文

我有两个收藏:新闻和订阅。每个新闻项目都有一个字符串数组 - “标签”。每个订阅者也有这样的“标签”。

订阅的新闻项目是具有订阅所具有的所有标签的项目,并且可能更多。新闻项目的订阅者是具有该项目的任何标签的订阅者,但不再有。

当我想要获取订阅者的新闻时,我在 Ruby MongoID 上执行这样的请求:

NewsItem.where(:tags.all => @subscribe.tags)

如何获取某个新闻项的所有订阅?

例如:

item.tags = ["foo", "bar"]

subscribe1.tags = ["foo"]
subscribe2.tags = ["bar"]
subscribe3.tags = ["foo", "bar"]
subscribe4.tags = ["foo", "bar", "baz"]

item.subscribes 应该给出 subscribes 1..3,但 subscribe4 不应该包含在内,因为它有一个“baz”标签,未包含在 item.tags 中

I have two collections: news and subscribes. Every news item has an array of strings - "tags". Every subscribe also has such "tags".

Subscribe's news items are items having all tags that subscribe has, and may be more. News item's subscribes are subscribes having any of this item's tags, but no any more.

When I want to get a Subscribe's news, I'm doing such request on Ruby MongoID:

NewsItem.where(:tags.all => @subscribe.tags)

How can I get all subscribes for some news item?

For example:

item.tags = ["foo", "bar"]

subscribe1.tags = ["foo"]
subscribe2.tags = ["bar"]
subscribe3.tags = ["foo", "bar"]
subscribe4.tags = ["foo", "bar", "baz"]

item.subscribes should give subscribes 1..3, but subscribe4 should not be included, because it has a "baz" tag that is not included in item.tags

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

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

发布评论

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

评论(2

爱殇璃 2024-11-10 11:12:27

根据您的描述,您并不真正想要 $all。相反,您正在寻找某种形式的 $subset 运算符。有 JIRA 请求 就是为了这样的事情,但目前尚未实现。

Based on your description, you don't really want an $all. Instead, you are looking for some form $subset operator. There is JIRA request for just such a thing, however it is not implemented at this time.

被你宠の有点坏 2024-11-10 11:12:27

您应该“在创建新站点时”执行匹配,因此您可以按需且频繁地执行此操作。扭转查询并

Subscriber.all_in(tags: news_item.tags)

查找具有新闻站点所有标签的订阅者。这是你想要的吗?

无论如何,对于很多订阅者来说,这很快就会变得非常密集。您可以使用延迟作业在后台处理它。您应该尝试展平数组或设置其他键以建立索引并加快搜索速度。

You should perform the matching "on newsitem creation", so you do this operation on-demand and frequently. Turn the query around and do

Subscriber.all_in(tags: news_item.tags)

to find subscribers having all the tags of the newsitem. Is this how you wanted it?

In any case, with many subscribers, this will quickly get very intensive. You may use delayed job to handle it in the background. You should experiment with flattening arrays or setting other keys in order to index and speed up search.

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