本文介绍了使用 MongoMapper 查询不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

我认为你需要下拉到 ruby​​ 驱动程序才能做到这一点,因为我认为你不能用 MongoMapper 本身做到这一点:

>

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

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

collection.distinct(key, query = nil)

您可以在此处

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?

解决方案

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

这篇关于使用 MongoMapper 查询不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:03