2

I currently have a collection of documents that each contain arrays pointing at other documents within that collection. I need to query this collection for documents where the ones nested in arrays contain a certain property. I hope this will explain my request more clearly:

if doc.list1[0].prop = 'foo' or doc.list2[0].prop = 'foo' then select doc

I have tried using .find() like this but to no avail.

{
    'doc.list1': 'foo',
    $or: [
        { 'doc.list2': 'foo' }
    ]
}

Am I on the right track? Because I don't think I am. This is the best I can gleam from the documentation.

Edit

Here is my actual query initialisation using the same layout as Thomas's suggestion.

var query = this.Word.find({
    $or: [
        { 'before.0.cleanWord': topic },
        { 'after.0.cleanWord': topic },
        { 'cleanWord': topic }
    ]
});
Olical
  • 39,703
  • 12
  • 54
  • 77

1 Answers1

3
{
  $or: [
    { 'doc.list1.0.prop': 'foo' }
    { 'doc.list2.0.prop': 'foo' }
  ]
}

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24or

Thomas Blobaum
  • 3,680
  • 1
  • 17
  • 16
  • Excellent, I cannot test until tomorrow but this looks promising. So this will search every nested document in the array, and not just the first? – Olical Dec 07 '11 at 23:04
  • Im having a little hard time understanding whats going on in your database. Is it an array of Object Ids? That should be a valid $or query either way though, the one you posted will always match doc.list1 = 'foo' and your $or statement is extraneous because it has only one element, its more like an AND. – Thomas Blobaum Dec 08 '11 at 00:03
  • Okay, I have appended my current code using your suggestion to my question. It still does not return any documents. Still debugging though. – Olical Dec 08 '11 at 14:13
  • 1
    My bad! It works with your code, the other problem was me not adding `cleanWord` to the schema. Thank you very much! – Olical Dec 08 '11 at 14:20