使用 MongoMapper 查询不同值

发布于 2025-01-01 18:34:26 字数 229 浏览 0 评论 0原文

如何使用 MongoMapper 查询distinct?我的查询是:

subscribedToThread = Comment.where(:subscribe_thread => 1).all

但这将返回许多具有相同 user_id 的对象。我只需要返回一个不同的user_id。这可能吗?

How do I query distinct with MongoMapper? My query is:

subscribedToThread = Comment.where(:subscribe_thread => 1).all

But this will return many objects with the same user_id. I need to return just a distinct user_id. Is this possible?

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

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

发布评论

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

评论(4

晚雾 2025-01-08 18:34:26

我认为您需要转到 ruby​​ 驱动程序才能执行此操作,因为我认为您无法使用 MongoMapper 本身执行此操作:

subscribedToThread = Comment.collection.distinct("user_id", {:subscribe_thread => 1})

在模型上调用集合方法会返回由 Ruby 驱动程序直接提供的集合因此您可以使用以下语法发出不同的查询:

collection.distinct(key, query = nil)

您可以阅读有关它的更多信息此处

I think you will need to drop down to the ruby driver in order to do this as I don't think you can do this with MongoMapper itself:

subscribedToThread = Comment.collection.distinct("user_id", {:subscribe_thread => 1})

Calling the collection method on a model returns the collection as would be provided by the Ruby driver directly so you can issue a distinct query using the syntax below:

collection.distinct(key, query = nil)

You can read more about it here

那请放手 2025-01-08 18:34:26

是的,你可以这样做:

subscribedToThread = Comment.where(:subscribe_thread => 1).fields(:user_id).all.compact!.unique!

这将清空除 user_id 之外的所有字段,然后你将 uniq! 删除,即删除所有双精度数,然后将 compact! 全部 nil

http://mongomapper.com/documentation/plugins/querying.html#fields

Yes, you can do so:

subscribedToThread = Comment.where(:subscribe_thread => 1).fields(:user_id).all.compact!.unique!

This will nil every field but user_id which you then uniq!,ie you remove all doubles and then compact! all nil

http://mongomapper.com/documentation/plugins/querying.html#fields

淡水深流 2025-01-08 18:34:26

试试这个

subscribedToThread = Comment.where(:subscribe_thread => 1).fields(:user_id).collect(&:user_id).uniq 

它会显示 uniq user_id 列表

Try this

subscribedToThread = Comment.where(:subscribe_thread => 1).fields(:user_id).collect(&:user_id).uniq 

It will show you list of uniq user_id

烂柯人 2025-01-08 18:34:26

对于原始帖子,请尝试以下操作:

subscribedToThread = Comment.where(:subscribe_thread => 1).distinct(:user_id)

当用户从列表中选择 type =“Other”并输入自定义名称时,我想计算不同条目的数量(以便我可以考虑向列表中添加更常见的选择)。

InteriorInfo.where(type: 'Other').distinct(:name).sort.each{|i| puts "%3d %s" % [InteriorInfo.where(name: i).count, i]}

结果(片段)

  6 AED
  1 Alarm Panel
 16 Basement Access

For the original post, try this:

subscribedToThread = Comment.where(:subscribe_thread => 1).distinct(:user_id)

I wanted to count the number of distinct entries when the user chose type = "Other" from the list and entered a custom name (so that I could consider adding more common choices to the list).

InteriorInfo.where(type: 'Other').distinct(:name).sort.each{|i| puts "%3d %s" % [InteriorInfo.where(name: i).count, i]}

Resulting in (snippet)

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