3

当我运行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”

4

2 回答 2

2

你可以这样做

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

于 2013-08-07T05:02:00.657 回答
0

到目前为止,我发现的唯一方法是直接使用 Mongo 驱动程序。

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

于 2013-08-09T00:36:07.007 回答