Trying to understand how to query "embedded" models with geddy.. help please! #549

javiercerdas opened this Issue Feb 21, 2014 · 5 comments


None yet

3 participants


We are successfully using querying mongo right now, with basic stuff like using all, first, etc... but we need to have a more "complex" query? (Maybe it's not that complex and we're just missing something basic here)
For example, we have a collection of 'contacts' in our mongoDB, and one of the properties of this Contact model is an array, called 'addresses' -> This is an array of Address models, which in turn have street address, city, state, country... how can I query something like this: Get me all the contacts, with an address in the city of "Atlanta"?

Thanks to anyone who can help clarify this for us. :)

ben-ng commented Feb 21, 2014

You can't do this right now because there would be ambiguity in the query object.


  ingredients: { or: [ 'cookies', 'milk' ] }

Model wouldn't know if the nested object is a query condition, or the object to be matched.

If we can figure out a non-ambiguous way to query object fields that would be good.


Ah ok, got it. Thanks for the reply Ben. One more question ;) I saw a reply where it was mentioned that we are able to access the mongo client in the wrapper, to execute any kind of query, using


but I don't understand how to run a query with that... I haven't been able to find anyone that has written an example of it... just found 2 references in different posts about it... but just that one line... can anyone provide an example of usage? Thanks.

molda commented Feb 21, 2014

Here you have it.

var mongo = geddy.model.adapters.mongo.client;
var db = mongo.db('localhost', 27017, 'your-db-name');
var posts = db.collection('posts');{a:'a'}, function(err, post){
   posts.find().toArray(function(err, p){
      console.log(p); // prints => [ { a: 'a', _id: 5307623938acc3540744a137 } ]

Thank you very much!

ben-ng commented Feb 21, 2014

Yes, as molda has shown it is possible to access the adapter directly to do this.

Ideally we would have a proper way to do this, especially since the latest postgres supports querying on JSON fields too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment