gpt4 book ai didi

c# - 从 MongoDB C# 中只获取指定的字段

转载 作者:可可西里 更新时间:2023-11-01 10:00:46 25 4
gpt4 key购买 nike

我的应用程序中有以下 POCO 类 -

public class Course
{
public String Title { get; set; }
public String Description { get; set; }
}

但是 mongodb 中的 Course 集合还有一些其他字段也包括那些。我正在尝试按如下方式获取数据-

var server = MongoServer.Create(connectionString);
var db = _server.GetDatabase("dbName");
db.GetCollection("users");

var cursor = Photos.FindAs<DocType>(Query.EQ("age", 33));
cursor.SetFields(Fields.Include("a", "b"));
var items = cursor.ToList();

我从 this post 得到了代码在计算器中。

但是它抛出一个异常-

"Element '_id' does not match any field or property of class"

我不想在我的 POCO 中使用“_id”字段。有帮助吗?

最佳答案

_id 默认包含在字段中。

您可以使用类似的方式排除它:

cursor.SetFields(Fields.Exclude("_id"))

关于c# - 从 MongoDB C# 中只获取指定的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28296183/

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