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

Closed
javiercerdas opened this Issue Feb 21, 2014 · 5 comments

Projects

None yet

3 participants

@javiercerdas

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
Contributor
ben-ng commented Feb 21, 2014

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

Consider:

{
  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.

@javiercerdas

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

geddy.model.loadedAdapters..client

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
Contributor
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');
posts.save({a:'a'}, function(err, post){
   posts.find().toArray(function(err, p){
      console.log(err);
      console.log(p); // prints => [ { a: 'a', _id: 5307623938acc3540744a137 } ]
   });
});
@javiercerdas

Thank you very much!

@ben-ng
Contributor
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