铁轨和Mongoid 独特的结果
考虑以下 mongo 集合的示例:
{"_id" : ObjectId("4f304818884672067f000001"), "hash" : {"call_id" : "1234"}, "something" : "AAA"}
{"_id" : ObjectId("4f304818884672067f000002"), "hash" : {"call_id" : "1234"}, "something" : "BBB"}
{"_id" : ObjectId("4f304818884672067f000003"), "hash" : {"call_id" : "1234"}, "something" : "CCC"}
{"_id" : ObjectId("4f304818884672067f000004"), "hash" : {"call_id" : "5555"}, "something" : "DDD"}
{"_id" : ObjectId("4f304818884672067f000005"), "hash" : {"call_id" : "5555"}, "something" : "CCC"}
我想查询此集合并仅获取每个“call_id”的第一个条目,换句话说,我试图根据“call_id”获取唯一的结果。 我尝试使用 .distinct 方法:
@result = Myobject.all.distinct('hash.call_id')
但生成的数组将仅包含唯一的 call_id 字段:
["1234", "5555"]
并且我也需要所有其他字段。 是否可以进行这样的查询?:
@result = Myobject.where('hash.call_id' => Myobject.all.distinct('hash.call_id'))
谢谢
Consider the following example of mongo collection:
{"_id" : ObjectId("4f304818884672067f000001"), "hash" : {"call_id" : "1234"}, "something" : "AAA"}
{"_id" : ObjectId("4f304818884672067f000002"), "hash" : {"call_id" : "1234"}, "something" : "BBB"}
{"_id" : ObjectId("4f304818884672067f000003"), "hash" : {"call_id" : "1234"}, "something" : "CCC"}
{"_id" : ObjectId("4f304818884672067f000004"), "hash" : {"call_id" : "5555"}, "something" : "DDD"}
{"_id" : ObjectId("4f304818884672067f000005"), "hash" : {"call_id" : "5555"}, "something" : "CCC"}
I would like to query this collection and get only the first entry for each "call_id", in other words i'm trying to get unique results based on "call_id".
I tried to use .distinct method:
@result = Myobject.all.distinct('hash.call_id')
but the resulting array will contain only the unique call_id fields:
["1234", "5555"]
and I need all the other fields too.
Is it possible to make a query like this one?:
@result = Myobject.where('hash.call_id' => Myobject.all.distinct('hash.call_id'))
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能简单地使用不同的返回文档(或子集)。根据文档,它仅返回基于给定键的不同值数组。但是你可以通过使用map-reduce来实现这一点
上面的代码是不言自明的,在map函数上我通过键
hash.call_id
对其进行分组并返回整个文档,以便它可以通过reduce函数进行处理。在reduce函数中,只需循环遍历分组结果集并从分组集中仅选取一项(在多个重复键值中 - 不同的模拟)。
最后创建一些测试数据
并运行此映射减少
您将获得不同集的第一个文档。您可以在 mongoid 中执行相同的操作,首先将 map/reduce 函数字符串化并像这样调用 mapreduce
希望它有帮助
You cannot simply return the document(or subset) by using the distinct. As per the documentation it only returns the distinct array of values based on the given key. But you can achieve this by using map-reduce
The above code is self explanatory, on map function i am grouping it by key
hash.call_id
and returning the whole doc so it can be processed by reduce funcition.On reduce function, just loop through the grouped result set and pick only one item from the grouped set (among the multiple duplicate key values - distinct simulation).
Finally create some test data
and running this map reduce
You get the first document of the distinct set. You can do the same in mongoid by first stringify the map/reduce functions and call mapreduce like this
Hope it helps