Olical
Olical

Reputation: 41352

Use mongoose to find by a nested documents properties

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 }
    ]
});

Upvotes: 2

Views: 3904

Answers (1)

Thomas Blobaum
Thomas Blobaum

Reputation: 3730

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

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

Upvotes: 2

Related Questions