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":"534186e5414fc34bc500000‌​3","un":"dummy"}] 

using mongo itself, return null values. have tried using pluck method in mongoid 3 ?

collection.all.pluck(:username) 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -