2013-08-07 54 views
3

当我运行User.only(:name).all或任何该查询的变种,我得到:Mongoid :: Criteria.only(:名称)返回所有领域零除名称

[#<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>] 

所有在我的文档返回具有零领域除了唯一功能的参数之外的值。

我想要得到的结果的东西有点像这样:

"data": [ 
    { 
    "id": "5008484" 
    }, 
    { 
    "id": "47402727" 
    }, 
    { 
    "id": "500154435" 
    }, 
    { 
    "id": "500598450" 
    }, 
    { 
    "id": "500655911" 
    }, 
    { 
    "id": "500666528" 
    }, 
    { 
    "id": "500700148" 
    }, 
    { 
    "id": "500973616" 
    }, 
    { 
    "id": "501130485" 
    }, 
    { 
    "id": "501254060" 
    }] 

而是 “ID”, “姓名”

回答

0

我发现的唯一方法是直接使用Mongo驱动程序。

Mongo::MongoClient.new(ENV['MONGOID_HOST'], ENV['MONGOID_PORT']).db(ENV['MONGOID_DATABASE']).collection('friends').find({}, {fb_id: 1})

2

你可以像下面这样做

{数据:用户.only(:name).all.map(&:attributes)}