Is there a way to unselect _id field in a query? (opposite of 'select' operator) [projection].

188 views
Skip to first unread message

Aleksey Izmailov

unread,
Sep 27, 2013, 10:42:12 PM9/27/13
to rogue...@googlegroups.com, michel dahdah
Hi all,

In rogue, field projection is implemented with select operator.

An example from https://github.com/foursquare/rogue/blob/master/rogue-lift/src/test/scala/com/foursquare/rogue/QueryTest.scala demonstrates this:

Venue.where(_.mayor eqs 1).select(_.legacyid).toString() must_== """db.venues.find({ "mayor" : 1}, { "legid" : 1})"""

Mongo by default always returns id unless it's unselected. Is there any way to unselect a field, in particular an "_id" field?


In mongo you can do it this way:

db.collection.find({}, {"_id": 0, "field" : 1});

I could not find anything in Rogue that looks like it.

Thanks


Jorge Ortiz

unread,
Sep 28, 2013, 3:59:42 AM9/28/13
to rogue...@googlegroups.com, michel dahdah
Cool! I didn't know an unselect operator existed. Rogue does not currently support this.

I think it'd be possible to add an .unselectId method for queries that already have a Select clause. It might be harder to unselect arbitrary fields from non-select queries.

--j


--
You received this message because you are subscribed to the Google Groups "rogue-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rogue-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Konrad Malawski

unread,
Sep 29, 2013, 5:03:47 AM9/29/13
to rogue...@googlegroups.com, michel dahdah

Hi guys,
I think it's doable so it got me thinking, if:
a) we want that feature (@jason?)
b) if we want it as .unselectId or .unselect(_.id) + only available on an Selected state of the query (should be doable AFAIR with the phantom types).

I think I like the second syntax better, even though it adds a bit more complexity... What do you think guys?

-- 
Konrad (PS: Yay, yosomite!)

Aleksey Izmailov

unread,
Sep 29, 2013, 11:49:51 PM9/29/13
to rogue...@googlegroups.com, michel dahdah
To me .unselect(_.id) looks nicer because it's an opposite of .select and "matches" mongo semantics. Additionally users might want to unselect other fields just like in Mongo.

Since Mongo allows only inclusion or exclusion of fields in projection clause users would have to say something like this:
  • .unselect(_.field1).unselect(_.field2) // keeps id by default
  • .select(_.field3).select(_.field4) // keeps id by default
  • .select(_.field3).select(_.field4).unselect(_.id) // get rid of id
  • .unselect(_.field1).select(_.field3) // Illegal !!!

I'm not sure how easy it's to make last line with illegal selectors not compile in Rogue. I never looked at how it's implemented but I'm very curious to see when I have time. :)

My 2 cents

Jorge Ortiz

unread,
Sep 30, 2013, 1:56:54 PM9/30/13
to rogue...@googlegroups.com, michel dahdah
The problem with unselecting arbitrary fields is what to use as the return type. We could return a Record type with those fields missing, but sometimes fields are marked as required in a Record, which could lead to confusing semantics...

Perhaps the correct approach is that something like .select(_.name, _.address) should automatically (under the hood) unselect the _id field since no one asked for it. We don't even have anywhere to put it so it gets discarded by the runtime anyway, it just takes up space over the wire.


Aleksey Izmailov

unread,
Sep 30, 2013, 7:28:37 PM9/30/13
to rogue...@googlegroups.com
George, that sounds good. In this case there is no way to unselect fields, and you are just selecting what you really want like in SQL. Maybe this is even more clear in terms of semantics.

Jason Liszka

unread,
Oct 1, 2013, 10:26:52 PM10/1/13
to rogue...@googlegroups.com
We can try that. My only concern is LiftQueryExecutor.scala:54, which explicitly references the _id field.

LiftQueryExecutorHelpers.setInstanceFieldFromDbo(inst, dbo, "_id")

Not entirely sure I remember what that line is for (I should have left a comment), but I can try it without and see what happens.


On Mon, Sep 30, 2013 at 7:28 PM, Aleksey Izmailov <izma...@gmail.com> wrote:
George, that sounds good. In this case there is no way to unselect fields, and you are just selecting what you really want like in SQL. Maybe this is even more clear in terms of semantics.
Reply all
Reply to author
Forward
0 new messages