当我运行User.only(:name).all
或该查询的任何变体时,我得到:
[#<User _id: 52003fb8487874985b
000003, created_at: nil, updated_at: nil, role_ids: nil, name: "Tom Amfeagfhbdge Bowersman", usernam
e: nil, fb_id: nil, fb_token: nil, email: nil, encrypted_password: nil, reset_password_token: nil, r
eset_password_sent_at: nil, remember_created_at: nil, sign_in_count: nil, current_sign_in_at: nil, l
ast_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>,
#<User _id: 5200405a487874fb550
00004, created_at: nil, updated_at: nil, role_ids: nil, name: "Carol Amfdheibbjbf Sadanwitz", userna
me: nil, fb_id: nil, fb_token: nil, email: nil, encrypted_password: nil, reset_password_token: nil,
reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: nil, current_sign_in_at: nil,
last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>,
#<User _id: 520040d14878746fe5
000005, created_at: nil, updated_at: nil, role_ids: nil, name: "Tom Amfdidheeidd Yangson", username:
nil, fb_id: nil, fb_token: nil, email: nil, encrypted_password: nil, reset_password_token: nil, res
et_password_sent_at: nil, remember_created_at: nil, sign_in_count: nil, current_sign_in_at: nil, las
t_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>]
我的文档中的所有字段都返回一个nil值,但唯一函数的参数除外。
我想得到的结果有点像这样:
"data": [
{
"id": "5008484"
},
{
"id": "47402727"
},
{
"id": "500154435"
},
{
"id": "500598450"
},
{
"id": "500655911"
},
{
"id": "500666528"
},
{
"id": "500700148"
},
{
"id": "500973616"
},
{
"id": "501130485"
},
{
"id": "501254060"
}]
而不是“id”,“name”
答案 0 :(得分:2)
你可以这样做
{data:User.only(:name).all.map(&amp;:attributes)}
答案 1 :(得分:0)
到目前为止我找到的唯一方法是直接使用Mongo驱动程序。
Mongo::MongoClient.new(ENV['MONGOID_HOST'], ENV['MONGOID_PORT']).db(ENV['MONGOID_DATABASE']).collection('friends').find({}, {fb_id: 1})