gpt4 book ai didi

c# - 从 Mongo DB 读取 "_t"字段

转载 作者:行者123 更新时间:2023-12-05 05:15:18 24 4
gpt4 key购买 nike

情况:

[BsonDiscriminator(RootClass = true)]
[BsonKnownTypes(typeof(Child), typeof(Sibling))]
public class Parent { ... }

public class Child : Parent { ... }

public class Sibling : Parent { ... }

假设3个类保存在同一个集合中,是否可以读取_t使用基于 Parent 的集合从 MongoDB 生成的字段类(database.GetCollection<Parent>)?

注意:我试图将 string[] 数组放在 Parent 中类,但字段 _t 返回 null

最佳答案

根据documentation ,您可以这样过滤:

var filter = new BsonDocument("_t", "Cat");

您还可以使用 LINQ OfType<Type>将列表过滤为仅一种类型,或者您可以测试 C# 对象类型以确定要执行的操作。

我不知道您是否可以直接“读取”字符串,但类型信息在 C# 中也可用,所以我不明白您如何需要它。

关于c# - 从 Mongo DB 读取 "_t"字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51709000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com