【问题标题】:Get only specified fields from MongoDB C#仅从 MongoDB C# 获取指定的字段
【发布时间】:2015-02-03 10:06:23
【问题描述】:

我的应用中有以下 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();

我在 stackoverflow 中从 this post 获得了该代码。

但它会引发异常-

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

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

【问题讨论】:

  • 我认为 _id 默认包含在内。您是否尝试过使用 cursor.SetFields(Fields.Exclude("_id")) ?

标签: c# mongodb


【解决方案1】:

_id 默认包含在字段中。

您可以通过以下方式排除它:

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

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多