rubymongodbmongodb-ruby

Mongodb-Ruby Limiting to specific fields


I'm having some problems limiting fields in result set from mongodb using the Ruby API.

I have a mongo collection where I am storing information about files. One of the fields is 'image_filename'. I am attempting to return the fields and the id for any record in the collection that has this field.

In the mongo shell this is accomplished by:

db.images.find({"image_filename": /./}, {image_filename: 1} )

This works great. But, when I try to do a similar query via the Ruby API

result = mongo.db.find("image_filename" => /./, :fields => ['_id', 'image_filename'])

I am only getting a null set. I presume I am doing something noobishly wrong, again. I've tried every conceivable variation of the above, can anyone point me in the right direction?

Thanks!


Solution

  • Are you using the collection?

    result = mongo.db['images'].find({"image_filename" => /./}, {:fields => ['_id', 'image_filename']})
    

    Also notice that I separated the query from the option hashes.