我在Mongodb中有一个Person的集合,其结构如下:
class Person
{
public string { get; set; }
public Address houseAddress { get; set; }
}
class Address
{
public string streetNumber { get; set; }
public City currentCity { get; set; }
}
class City
{
public string Name { get; set; }
}
我想只选择人的城市子字段。我尝试使用排除和包括但不工作。有人会告诉我在C#
中执行此操作的正确方法是什么我试过这段代码:
string jsonQuery = @"{}, {'houseAddress.currentCity' : 1, _id:0}";
BsonDocument query = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonQuery);
var sQuery = new QueryDocument(query);
var cities = dbConnection.Person.FindAs<Person>(sQuery).SetFields(Fields.Include("currentCity"));