[英]Get only specified fields from MongoDB C#
I have the following POCO class in my app - 我的应用程式中有以下POCO类别-
public class Course
{
public String Title { get; set; }
public String Description { get; set; }
}
But the Course
collection in mongodb has some other fields also including those. 但是mongodb中的
Course
集合还包含其他一些字段。 I am trying to get data as follows- 我正在尝试获取数据,如下所示:
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();
I have got that code from this post in stackoverflow. 我从stackoverflow的这篇文章中获得了该代码。
But it throws an exception- 但这引发了一个例外-
"Element '_id' does not match any field or property of class"
I don't want '_id' field in my POCO. 我不需要POCO中的“ _id”字段。 Any help?
有什么帮助吗?
_id
is included in Fields by default. _id
默认包含在字段中。
You can exclude it by using something like: 您可以使用以下方法将其排除:
cursor.SetFields(Fields.Exclude("_id"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.