Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One of the ways where you can query something like as follows:</p> <pre><code>db.people.find({name : 'Joe', 'path' : { $all : [ObjectId("4e0fcf1722b7a9439200002e"), ObjectId("4e0fcf1622b7a9439200002b")]}}) </code></pre> <p>However the drawback I think of this is:</p> <ul> <li>You don't get to substitute the object relations/joins directly in mongo shell. You have to use the ObjectId object</li> <li>The $all clause does not necessitate that the order of path is strictly same, which means that a person with name "Joe" referenced by path "hometown/town/" would come up as well as "Joe" from "tome/hometown".</li> </ul> <p>I would presume that second one may be a deal breaker. Also, I am presuming that mongoid in several cases pass across the query options directly to mongodb (or atleast there are ways to do that). Hence it should be possible to do a search in ruby code using given query above. </p> <p>Nevertheless, I'll do some more re-search on this and post my findings back.</p> <p>Hope it helps.</p> <p>Edit</p> <p>To alleviate the second problem above there is also another way to query a person with a specific path. Find it below:</p> <pre><code>db.people.find({name : 'Joe', 'path.0' :ObjectId("4e0fcf1722b7a9439200002e"), 'path.1':ObjectId("4e0fcf1622b7a9439200002b")}) </code></pre> <p>This would ensure that path is exactly what you are looking for. However this works in mongodb shell and you may still need to figure out, how mongoid can run an equivalent of this. Plus you may have to construct this query dynamically to create a path for person and that (for deep nested people) may just become long and ugly.</p> <p>I would suggest skimming through following links on mongodb documentation to get a better understanding.</p> <p><a href="http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29" rel="nofollow">http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29</a></p> <p><a href="http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray" rel="nofollow">http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray</a></p> <p>I hope this is what you were looking for.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload