MongoDB/MongoID多键特殊查询
我有两个收藏:新闻和订阅。每个新闻项目都有一个字符串数组 - “标签”。每个订阅者也有这样的“标签”。
订阅的新闻项目是具有订阅所具有的所有标签的项目,并且可能更多。新闻项目的订阅者是具有该项目的任何标签的订阅者,但不再有。
当我想要获取订阅者的新闻时,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的描述,您并不真正想要
$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.您应该“在创建新站点时”执行匹配,因此您可以按需且频繁地执行此操作。扭转查询并
查找具有新闻站点所有标签的订阅者。这是你想要的吗?
无论如何,对于很多订阅者来说,这很快就会变得非常密集。您可以使用延迟作业在后台处理它。您应该尝试展平数组或设置其他键以建立索引并加快搜索速度。
You should perform the matching "on newsitem creation", so you do this operation on-demand and frequently. Turn the query around and do
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.