9

I'm trying to get a subset of fields from MongoDB with a query made in Ruby but it doesn't seem to work. It doesn't return any results

This is the ruby code:

coll.find("title" => 'Halo', :fields => ["title", "isrc"]) #this doesn't work

If I remove the fields hash, it works, returning the results with all the fields

coll.find("title" => 'Halo') #this works

Looking at the mongodb console the first query ends-up on the mongodb server like this:

{ title: "Halo", fields: [ "title", "isrc" ] }

If I try to make the query from the mongo client console, it works, I get the results and the subset. I make the query like this:

db.tracks.find({title: 'Halo'}, {title:1,isrc:1})

What could be the problem? I've been looking for a solution for this for a couple of hours now.

2
  • 1
    Can you try: coll.find({"title" => 'Halo'}, :fields => ["title", "isrc"]) Commented May 25, 2011 at 23:50
  • Please note that current ruby driver changed the name of the option key from :fields to :projection. api.mongodb.org/ruby/current/Mongo/…
    – Ernest
    Commented Mar 31, 2016 at 22:40

4 Answers 4

13

As of Sep, 2015, these other answers are outdated. You need to use the projection method: #projection(hash)

coll.find({"title" => 'Halo'}).projection({title: 1, isrc: 1})
1
  • 2
    Or coll.find({"title" => "Halo"},{projection: {title: 1, isrc: 1}}).
    – Ernest
    Commented Mar 31, 2016 at 22:36
12

The query should look like

collection.find(selector = {}, opts = {})

Query the database

In your case it is

coll.find({"title" => 'Halo'}, {:fields => ["title", "isrc"]})

But still remains a problem, the ruby-driver ignores the condition of "fields", and returns all the fields! :\

5
  • This worked for me, the ruby-driver didn't ignore the "fields" option. Thanks!
    – Nick Dima
    Commented May 27, 2011 at 8:58
  • Yes, I was mistaken, the ruby-driver don't ignore the :fields option. This issue of ODM, I use Mongoid, but in MongoMapper it is also present. You can read about this problem here.
    – Vladimir
    Commented May 27, 2011 at 11:57
  • It may be useful to note that according to the MongoDB documentation you always get the _id unless you specifically exclude it with field negation. mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields
    – jj0b
    Commented May 4, 2012 at 5:27
  • Latest ruby driver (as linked) changed fields option name to projection. So of course fields key is ignored.
    – Ernest
    Commented Mar 31, 2016 at 22:38
  • @Vladimir in Mongoid use Model.only(:title, :isrc).where(...) and it will correctly construct your object, but throw if you try to access another field
    – Jaffa
    Commented May 28, 2016 at 9:04
10

This query will return only the title and isrc for a doc that has the title "Halo":

coll.find({"title" => 'Halo'},{:fields => {"_id" => 0, "title" => 1, "isrc" => 1}})

Note the use of a Hash for the fields where the keys are the field names and the values are either 1 or 0, depending on whether you want to include or exclude the given field.

1
  • 1
    Thanks - I've been trying to find how to negate fields with the Ruby driver (a projection specifying to not return the _id field). That doesn't seem to be well-documented for the Ruby driver.
    – quux00
    Commented Jun 9, 2012 at 22:30
3

You can use the below query

coll.find({"title" => 'Halo'}).projection({title: 1, isrc: 1, _id: 0})

if you don't want _id, to be retrieved in case.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.