Partial Model Population by DAO

20 views
Skip to first unread message

Neil Chaudhuri

unread,
Jun 8, 2014, 7:49:08 PM6/8/14
to scala...@googlegroups.com
Just curious if there is a way to only populate a portion of the model class. I'm aware of projections, but I am curious if it is possible to return the model class itself populated with a subset of components rather than the individual components themselves.

Trying projection[MyModel](MongoDBObject("field" -> field), "_id") didn't work for me.

Thanks.

rose katherine toomey

unread,
Jun 8, 2014, 8:07:21 PM6/8/14
to scala...@googlegroups.com
Hi Neil,

The answer is: yes. As long as the fields you're not bringing back all have default values, you can include or exclude whatever you like.  This can really speed up retrieval. 

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

Neil Chaudhuri

unread,
Jun 8, 2014, 8:09:38 PM6/8/14
to scala...@googlegroups.com
That was why I was looking into it. Now I'm going to check all my excluded fields have defaults.

Thanks.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-salat+unsubscribe@googlegroups.com.

Neil Chaudhuri

unread,
Jun 8, 2014, 9:16:59 PM6/8/14
to scala...@googlegroups.com
I have added defaults for all my fields, but I still can't get a partial projection to work.

This does work: MyModelDao.projection[Files](MongoDBObject("uniqueIdentifier" -> "identifier"), "files")

where Files is a component case class of MyModel.

This does not work: MyModelDao.projection[MyModel](MongoDBObject("uniqueIdentifier" -> "identifier"), "")

And by "not work," I mean the preceding call returns None while the other returns Some.

If we assume I haven't missed a default value somewhere, can you point out where my call is flawed? I have a feeling empty string (I also tried "{}" and "*") isn't the way to go.

Thanks.

rose katherine toomey

unread,
Jun 8, 2014, 10:01:07 PM6/8/14
to scala...@googlegroups.com
You don't need to project - just do a find it findOne, the first argument is the query and the second is the inclusions/exclusions. See the docs for the syntax since my phone will mangle it for sure....
To unsubscribe from this group and stop receiving emails from it, send an email to scala-salat...@googlegroups.com.

Neil Chaudhuri

unread,
Jun 8, 2014, 11:46:27 PM6/8/14
to scala...@googlegroups.com
I've scoured the documentation, Scaladoc, and the tests, but whatever I try in the REPL doesn't work for me (in various ways). Please let me know the syntax at your convenience.

Thanks.

Neil Chaudhuri

unread,
Jun 12, 2014, 4:37:30 PM6/12/14
to scala...@googlegroups.com
Any chance you can give me the syntax for a partially loaded model? Or at least point me to a resource (doc, test, whatever) that shows an example?

Thanks.

rose katherine toomey

unread,
Jun 12, 2014, 5:01:11 PM6/12/14
to scala-salat
Hi Neil,

http://docs.mongodb.org/manual/reference/method/db.collection.find/#db.collection.find

object Bar {
  val empty = Bar(/*etc*/)
}
case class Bar(/* bar stuff*/)
object Baz {
  val empty = Baz(/*etc*/)
}
case class Baz(/* baz stuff */)
case class Foo(_id: ObjectId, bar: Bar = Bar.empty, baz: Baz = Baz.empty)

// whitelist: bring back foo documents with just _id and bar fields
FooDAO.find(MongoDBObject(/*query*/), MongoDBObject("bar" -> 1, "_id" -> 1))

// blacklist: bring back foo documents without baz field
FooDAO.find(MongoDBObject(/*query*/), MongoDBObject("baz" -> 0))

Does this help?

Best,
Rose






To unsubscribe from this group and stop receiving emails from it, send an email to scala-salat...@googlegroups.com.

Neil Chaudhuri

unread,
Jun 12, 2014, 6:26:11 PM6/12/14
to scala...@googlegroups.com
Yes. That worked great. Thanks for your help, Rose. 


--
You received this message because you are subscribed to a topic in the Google Groups "scala-salat" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-salat/pQK8U_i78EU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-salat...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Neil Chaudhuri, M.S., PMP, CSM, CSP
President, Vidya
Mobile: 703.785.8855
Twitter: @VidyaSource
Reply all
Reply to author
Forward
0 new messages