Inheritance in MongoDb: how to request instances of defined type

后端 未结 5 1948
时光说笑
时光说笑 2021-02-10 16:41

This is how I used to utilize inheritance in Entity Framework (POCO):

ctx.Animals // base class instances (all instances)
ctx.Animals.OfType  // inher         


        
5条回答
  •  旧巷少年郎
    2021-02-10 17:03

    Take a look on the documentation http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/#scalar-and-hierarchical-discriminators

    The main reason you might choose to use hierarchical discriminators is because it makes it possibly to query for all instances of any class in the hierarchy. For example, to read all the Cat documents we can write:

    var query = Query.EQ("_t", "Cat");
    var cursor = collection.FindAs(query);
    foreach (var cat in cursor) {
        // process cat
    }
    

提交回复
热议问题