文档菜单
文档首页
/ / /
C#/.NET
/ / /

指定要返回的字段

在本页

  • 概述
  • 示例数据
  • 投影类型
  • 指定要包含的字段
  • 排除_id 字段
  • 指定要排除的字段
  • 附加信息
  • API 文档

在本指南中,您可以了解如何使用投影来指定从读取操作返回哪些字段。投影是一个文档,它指定MongoDB从查询中返回哪些字段。

本指南中的示例使用的是sample_restaurants.restaurants 集合,来自Atlas 示例数据集。要了解如何创建免费的 MongoDB Atlas 集群并加载数据集,请参阅快速入门.

您可以使用投影来指定返回文档中包含的字段,或指定要排除的字段。

在指定要包含的字段时,所有其他字段都将隐式排除(除默认包含的 _id 字段外)。您不能在单个投影中组合包含和排除语句,除非您正在排除 _id 字段。

要从返回的文档中删除 _id 字段,必须显式排除它。

要指定结果中要包含的字段,将 Project() 方法链接到 Find() 方法。调用 Project() 方法时,必须将投影定义作为参数传递。您可以使用 Builders<T>.Projection.Include() 方法构建投影定义,并将要包含的字段名作为参数传递。此方法可以链接到包含多个字段。

以下示例使用 Find() 方法查找所有 name 字段值为 "Emerald Pub" 的餐厅。然后,代码调用 Project() 方法指示查找操作在结果中包含 namecuisine 字段。

var filter = Builders<BsonDocument>.Filter.Eq("name", "Emerald Pub");
var projection = Builders<BsonDocument>.Projection
.Include("name")
.Include("cuisine");
var results = collection.Find(filter).Project(projection).ToList();
foreach (var result in results)
{
Console.WriteLine(result.ToJson());
}
{ "_id" : ObjectId("..."), "cuisine" : "American", "name" : "Emerald Pub" }
{ "_id" : ObjectId("..."), "cuisine" : "American", "name" : "Emerald Pub" }

当指定要包含的字段时,您也可以从返回的文档中排除 _id 字段。

以下示例运行与上一个示例相同的查询,但排除了投影中的 _id 字段。

var filter = Builders<BsonDocument>.Filter.Eq("name", "Emerald Pub");
var projection = Builders<BsonDocument>.Projection
.Include("name")
.Include("cuisine")
.Exclude("_id");
var results = collection.Find(filter).Project(projection).ToList();
foreach (var result in results)
{
Console.WriteLine(result.ToJson());
}
{ "cuisine" : "American", "name" : "Emerald Pub" }
{ "cuisine" : "American", "name" : "Emerald Pub" }

要指定要排除的结果字段,将 Project() 方法链接到 Find() 方法。您可以使用 Builders<T>.Projection.Exclude() 方法并传递要排除的字段名称作为参数来排除投影中的字段。此方法可以链接以排除投影中的多个字段。

以下示例使用 Find() 方法查找所有名称字段值为 "Emerald Pub" 的餐厅。然后它使用投影排除返回文档中的 cuisine 字段。

var filter = Builders<BsonDocument>.Filter.Eq("name", "Emerald Pub");
var projection = Builders<BsonDocument>.Projection
.Exclude("cuisine");
var results = collection.Find(filter).Project(projection).ToList();
foreach (var result in results)
{
Console.WriteLine(result.ToJson());
}
{ "_id" : ObjectId("..."), "address" : { "building" : "308", "coord" : [-74.008493599999994, 40.725807199999998], "street" : "Spring Street", "zipcode" : "10013" }, "borough" : "Manhattan", "grades" : [{ "date" : ISODate("2014-02-24T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-08-26T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-03-04T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-06-25T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-12-23T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-07-26T00:00:00Z"), "grade" : "C", "score" : 32 }], "name" : "Emerald Pub", "restaurant_id" : "40367329" }
{ "_id" : ObjectId("..."), "address" : { "building" : "18301", "coord" : [-73.791184999999999, 40.740119999999997], "street" : "Horace Harding Expressway", "zipcode" : "11365" }, "borough" : "Queens", "grades" : [{ "date" : ISODate("2014-05-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-04-30T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-03-01T00:00:00Z"), "grade" : "A", "score" : 13 }], "name" : "Emerald Pub", "restaurant_id" : "40668598" }

要了解有关投影的更多信息,请参阅MongoDB服务器手册中的字段投影指南

要了解更多关于本指南中讨论的任何函数或类型的信息,请参阅以下API文档

返回

检索数据