napfernandes
napfernandes

Reputation: 1359

MongoDB - Querying nested documents automatically

I'm wondering if there is any annotation or special method that I can use to query nested documents automatically. I have a class Queue that has a list of items. When querying a queue, I'd like to retrieve the items related to it, but I'm getting null.

public class Queue 
{
    public string Id { get; set; }
    public string Name { get; set; }
    public List<Item> Items { get; set; }
}

I'm querying a queue by its id using the following command:

var filter = Filter.Eq(queue => queue.Id, queueId);
return Collection.Find(filter).FirstOrDefaultAsync();

Is it an automatic way to do that or I need to go manually and query the item list based on the code above?

Thank y'all!

Upvotes: 0

Views: 99

Answers (1)

BOR4
BOR4

Reputation: 630

Does this work?

 public async Task<List<Item>> GetItemsFromQueue(string queueId)
 { 
    return await Collection.Find(queue => queue.Id == queueId)
        .Project(new ProjectionDefinitionBuilder<Queue>().Expression(q => q.Items))
        .FirstOrDefaultAsync();
}

Upvotes: 1

Related Questions