简体   繁体   中英

Querying hierarchical data on MongoDb

I have a hierarchical structure on MongoDb like the example bellow:

class Employee
{
    Guid Id {get;set;}
    Guid ParentId {get;set;}
    Guid[] ChildrenId {get;set;}
    Schedules[] Schedules {get;set;}
}

And I need to write a function that returns all of the schedules, or the count of them for all that are bellow one given Id (not just the direct children, but all of the hierarchy, which doesn't have a maximum number of layers).

I was able to do that using a recursive function, but that yields lots of queries to the database and bring a lot of data I do not want, for example, I need to bring all Employees, to just count their schedules based on some rule.

I want to know if there's a simpler or more performatic way to do this kind of data analysis.

I'm using MongoDb with the C# driver 2.1.

You can select fields to return by using

cursor.SetFields(Fields.Include("Shedules", ...));

Or you can use MongoDB aggregation api. It's quite difficult, but you just need to read documentation carefully

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM