Rails Mongoid `pluck` 等价物? [英] Rails Mongoid `pluck` equivalent?

查看:95
本文介绍了Rails Mongoid `pluck` 等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 应用程序中,我使用 MongoID 和 Elasticsearch 河流进行文本搜索.

In my Rails app I am using MongoID with a Elasticsearch river for text search.

对于 @devices_with_config = ConfigTextSearch.search params[:device_id]我只想提取查询中匹配记录的 device_id 字段.在 mongoID 3.1.0 中,我可以只使用 Band.all.pluck(:name).但不幸的是,我坚持使用 3.0.23.

For @devices_with_config = ConfigTextSearch.search params[:device_id] I want to extract just the device_id fields for the matching records in the query. In mongoID 3.1.0 I could just use Band.all.pluck(:name). But unfortunately, I am stuck with 3.0.23.

我看到有一个类似的轻便摩托车表达式 collections[:bands].find.select(name: 1)...但我是 Rails 新手,不知道该怎么做在控制器或模型中使用轻便摩托车表达式.

I see there is a similar moped expression collections[:bands].find.select(name: 1)...but I am new to rails and can't figure out how I would use a moped expression in a controller or model.

关于如何在与 MongoId 匹配的情况下提取device_id"字段的任何想法?

Any Ideas on how I can just extract the 'device_id' field in matches with MongoId?

推荐答案

Mongoid 现在有一个 pluck 功能.

Mongoid has a pluck function now.

mongoid#pluck

这篇关于Rails Mongoid `pluck` 等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆