在 mongodb 中,您可以使用类似的命令
db.sessions.distinct("Ip",{ 'Application': '123'})
这将返回所选应用程序的所有唯一 IP。如何通过 Mongoid 做到这一点?
我试图在distinct
函数中传递 2 个参数,但它失败并出现异常'ArgumentError: wrong number of arguments (2 for 1)'
Distinct in Mongoid takes one argument -- the field you wish to filter distinct on. So in your case, you could chain a where
clause w/a distinct
like so:
YourModel.where(Application: '123').distinct(:Ip)
This would produce a collection of distinct YourModel's by field Ip where the field Application is equal to '123'.
请向您展示完整的不同查询。我尝试遵循用户集合的语法(3 个名为 Bob 的文档):
db.users.distinct("_id", {name: "Bob"})
它的工作原理:
[
ObjectId("5121792d499af102889f2576"),
ObjectId("5121792e499af102889f2577"),
ObjectId("5121792f499af102889f2578")
]
我的 MongoDB 版本是 2.2.0