ruby on rails 3 - MongoDB with MongoID Fetching few fields in Find Query -
i'm trying write restful web api's, have collection called community, , has 10 fields. in getcommunityapi call want pass on necessary information mobile device.
i've referred blog example, has suggested following mongodb query
$db->users->find({}, {username => 1});
i've written mongoid search
@result=@result.find({},{:username=>1})
but gives me error not able find valid document id = {}
i've tried
@result=@result.only({:username=>1})
but instead of returning username returns making other field null.
{"_id":"534186e5414fc34bc5000002","ad":null,"ae":null,"cat":null...and on}
my question is, in order make api calls efficient. consuming least amount mobile data possible send fetch data mongodb in following format
{"_id":"534186e5414fc34bc5000002"}
this approach make api's sending valid information, , mobile network friendly. i'm expecting 1000's of object in single api call.
also possible output in format ?
[{"_id":"534186e5414fc34bc5000002","un":"name"},{"_id":"534186e5414fc34bc5000003","un":"dummy"}]
using mongo itself, return null
values. have tried using pluck
method in mongoid 3 ?
collection.all.pluck(:username)
Comments
Post a Comment