想象一下,我将以下类设置存储在MongoDB集合中:
public class Person
{
public ObjectId _id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Person> Children { get; set; }
}
因此,集合中的单个文档可能看起来像这样:
{
"_id" : ObjectId("<<myObjectId>>"),
"FirstName" : "James",
"LastName" : "Bond",
"Children" : [
{
"_id" : ObjectId("<<myObjectId>>"),
"FirstName" : "Peter",
"LastName" : "Bond",
"Children" : null
},
{
"_id" : ObjectId("<<myObjectId>>"),
"FirstName" : "Thomas",
"LastName" : "Bond",
"Children" : null
},
{
"_id" : ObjectId("<<myObjectId>>"),
"FirstName" : "Henry",
"LastName" : "Bond",
"Children" : null
}
]
}
我现在想从詹姆斯的孩子们那里找到名为托马斯的孩子。我可以通过以下预测来实现:
var projection = Builders<Person>
.Projection
.ElemMatch(person => person.Children, child => child.LastName == "Bond")
.Exclude(person => person._id);
当我现在将FindOptions指定为FindOptions<Person, Person>
时,结果是:
{
"_id" : ObjectId("0000000000000"),
"FirstName" : null,
"LastName" : null,
"Children" : [
{
"_id" : ObjectId("<<myObjectId>>"),
"FirstName" : "Thomas",
"LastName" : "Bond",
"Children" : null
}
]
}
对我来说,这不是理想的选择,因为我希望得到一个IEnumerable<Person>
。我知道我可以根据收到的结果自己创建它,但是我想知道是否对此有本地支持?
我将MongoDB.Driver 2.7.0与版本为4.0.2的MongoDB社区版一起使用。
谢谢您的帮助!