selected only specific fields

567 views
Skip to first unread message

Houdini

unread,
Nov 5, 2010, 9:59:02 PM11/5/10
to MongoMapper
When I use User.fields(:id, :name) it will return all 30 fields, but
all others values are null.
But really I'd like to see only smth like {:id => 15, :name => 'Joe'}
Is it possible?

John Nunemaker

unread,
Nov 6, 2010, 6:07:57 PM11/6/10
to mongo...@googlegroups.com
See the previous discussion:


--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en

Jon Kern

unread,
Nov 6, 2010, 8:51:44 PM11/6/10
to mongo...@googlegroups.com
Houdini, what "jam" are you trying to get out of by using fields? and what mechanism are you using to "see" them?

why don't you use a custom to_s that strips nils? or a custom UI? or a custom view class?

i have used the .fields option once in a while... when i just want a true subset -- like if i am dumping out a CSV.


But frankly it isn't all that necessary of a feature -- maybe I am missing something?

John, Is there any gain in performance by not returning all fields? (I know, I could benchmark it :-)

And yes, i get that data size would benefit. But, if you have a gigantic data-blob class with 100 fields that are rarely used in totality, I can see where auto-compacting would be nice. (But then, I submit that maybe your design could stand some refactoring?)

How do people usually use .fields, and why?
jon
blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA

Houdini said the following on 11/5/10 9:59 PM:

Houdini

unread,
Nov 6, 2010, 9:37:56 PM11/6/10
to MongoMapper
Main thing I really need it is to send object to client via json.
For example: search. I need only name actually, but user get really
gigantic objects.

Selecting only fields you want is real problem in mongo. About a year
ago I submit commit to ruby mongo driver to disable fields you don't
want to see in model, before if you have 40 fields and you would like
to send to client only 39, ten you should put all 39 fields in a big
array.

At my situation I think best way is to use mongo driver directly.

I guess it is hard to fix now, as mongomapper load keys before doing
anything else.

On 7 ноя, 03:51, Jon Kern <jonker...@gmail.com> wrote:
> Houdini, what "jam" are you trying to get out of by using fields? and what mechanism are you using to "see" them?
> why don't you use a custom to_s that strips nils? or a custom UI? or a custom view class?
> i have used the .fields option once in a while... when i just want a true subset -- like if i am dumping out a CSV.But frankly it isn't all that necessary of a feature -- maybe I am missing something?
> John, Is there any gain in performance by not returning all fields? (I know, I could benchmark it :-)
> And yes, i get that data size would benefit. But, if you have a gigantic data-blob class with 100 fields that are rarely used in totality, I can see where auto-compacting would be nice. (But then, I submit that maybe your design could stand some refactoring?)
> How do people usually use .fields, and why?jon blog:http://technicaldebt.comtwitter:http://twitter.com/JonKernPA

John Nunemaker

unread,
Nov 6, 2010, 10:13:11 PM11/6/10
to mongo...@googlegroups.com
Just select only the fields and then use :only in to_json.

Person.fields(:name).all.to_json(:only => :name)

Houdini

unread,
Nov 7, 2010, 5:57:22 PM11/7/10
to MongoMapper
Thanks
There is a lot of ways to solve this problem, but this way is not DRY

Jamie Orchard-Hays

unread,
Nov 8, 2010, 9:53:37 AM11/8/10
to mongo...@googlegroups.com
I'm not sure I agree. Requesting specific fields to be returned from the database is not the same as removing access to those attributes on the Ruby class.

I was confused the first time I requested only certain fields from the db and saw others output with the class instances. But once I thought about it, it made sense to me. It seems to me that I was confusing/conflating the data returned from the database with the enclosing class instances.

What do you all think?

Jamie

Kevin Lawver

unread,
Nov 8, 2010, 9:57:10 AM11/8/10
to mongo...@googlegroups.com
I think there's a big difference between selecting and exporting. The desire seems to be to only export specific fields. I don't see the connection between that and selecting a specific set of fields. You could easily write an export method that's both DRY and that gives you just the fields you want in a Hash, JSON or XML formats.

To me the only reason to select a subset of fields would be to avoid transmitting large fields across the wire if you're not going to use them. If that's not the case then I don't see anything blasphemous about selecting everything and exporting just what you want.

Kevin Lawver
http://lawver.net & http://ficly.com


Jon Kern

unread,
Nov 8, 2010, 4:04:14 PM11/8/10
to mongo...@googlegroups.com
well, I am still a newbie to all this, but that never stopped me from venturing an opinion before <g>, and reserving the right to change my mind in the face of logical opposition.

I think the following about the .fields behavior
  • it is correct as-is
  • if you want SQL-style select to return limited columns per records, use, umm, SQL :-)
  • if you want special behavior regarding subset of fields in MM, use object-oriented principles
    • You can even think "Data Transfer Object"
    • You could customize your original model to return special representations of itself
    • You could create child classes I suppose...
At any rate, if all you needed was a list of names from the User collection, then I probably would not expect that a list of "User" objects would be automatically smart enough to limit itself to only one property. At least not out of the box.

However, I would think one should be able to easily create a "Pick List" style return result with some straightforward coding -- and I think that is what we have available for us today with mongomapper and .fields, etc.
Jamie Orchard-Hays said the following on 11/8/10 9:53 AM:
Reply all
Reply to author
Forward
0 new messages