使用 MongoMapper 查询不同值
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您需要转到 ruby 驱动程序才能执行此操作,因为我认为您无法使用 MongoMapper 本身执行此操作:
在模型上调用集合方法会返回由 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:
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
是的,你可以这样做:
这将清空除 user_id 之外的所有字段,然后你将
uniq!
删除,即删除所有双精度数,然后将compact!
全部 nilhttp://mongomapper.com/documentation/plugins/querying.html#fields
Yes, you can do so:
This will nil every field but user_id which you then
uniq!
,ie you remove all doubles and thencompact!
all nilhttp://mongomapper.com/documentation/plugins/querying.html#fields
试试这个
它会显示 uniq user_id 列表
Try this
It will show you list of uniq user_id
对于原始帖子,请尝试以下操作:
当用户从列表中选择 type =“Other”并输入自定义名称时,我想计算不同条目的数量(以便我可以考虑向列表中添加更常见的选择)。
结果(片段)
For the original post, try this:
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).
Resulting in (snippet)