C# 如何使用MongoDb.net驱动程序为集合中的所有文档获取几个特定字段

C# 如何使用MongoDb.net驱动程序为集合中的所有文档获取几个特定字段,c#,.net,mongodb-.net-driver,bson,C#,.net,Mongodb .net Driver,Bson,我正在使用“MongoDB.Driver”Version=“2.9.3”。我正在尝试仅请求指定字段。 我需要执行休养请求(pypline的作品): 我需要创建Bson文档,我可以使用该文档查找具有受限字段的指定数据。 到目前为止,我试图从Json解析到Bson var doc = BsonDocument.Parse("{ 'geometry' : { '$nearSphere' : { '$geometry' : { 'type' : 'Point', 'coordinates' :" +

我正在使用“MongoDB.Driver”Version=“2.9.3”。我正在尝试仅请求指定字段。 我需要执行休养请求(pypline的作品):

我需要创建Bson文档,我可以使用该文档查找具有受限字段的指定数据。 到目前为止,我试图从Json解析到Bson

var doc = BsonDocument.Parse("{ 'geometry' : { '$nearSphere' : { '$geometry' : { 'type' : 'Point', 'coordinates' :" +
                    " [" + geolocation.Longitude.ToString(culture) + "," + geolocation.Latitude.ToString(culture) + "] }, '$minDistance' : 0, '$maxDistance' : 10 } }}, " +
                    " {'properties.be_id' : 1,'properties.rel_lttr' : 1 ,'properties.rp_co' : 1, 'properties.wenum' : 1}}}");
然后使用查找方法:

SegmentResult.AddRange(RScollection.Find<Segment>(doc).ToList());
SegmentResult.AddRange(RScollection.Find(doc.ToList());
这种方法似乎不起作用。
如果有人能帮我解决这个问题,我将不胜感激。

这是一个简单的控制台应用程序

using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;

public class Program
{           
    public static void Main()
    {
            var tempPipeline = new List<BsonDocument>();

            List<string> selectFields = new List<string>();
            selectFields.Add("<your requested field 1>");
            selectFields.Add("<your requested field 2>");

            var projection = new Dictionary<string, dynamic>();

            //To include fields: Specify the field name and set to 0 in the project document.
            //Ex:- Exclue _id field
            if (!selectFields.Contains("_id"))
            {
                projection.Add("_id", 0);
            }
            //Only the fields specified in the project document are returned. The _id field is returned unless it is set to 0 in the Project document.

            //To include fields: Specify the field name and set to 1 in the project document.

            foreach (var field in selectFields)
            {
                projection.Add(field, 1);       
                //or else
                //projection.Add(field, $"${field}");
            }


            var projectStage = new BsonDocument("$project", projection.ToBsonDocument());
            tempPipeline.Add(projectStage.ToBsonDocument());

            PipelineDefinition<BsonDocument, BsonDocument> aggregatonPipeline = tempPipeline;

            var cursor =  GetDatabase().GetCollection<BsonDocument>("<your collection>").Aggregate(aggregatonPipeline);

           IList<dynamic> results = new List<dynamic>();

            while (cursor.MoveNext())
            {
                foreach (var document in cursor.Current)
                {
                    results.Add(BsonSerializer.Deserialize<dynamic>(document));
                }
            }
            cursor.Dispose();

            results.ToList();
    }

    public static IMongoDatabase GetDatabase()
        {
            var settings = new MongoClientSettings
            {
                // setup your db settings
            };

            var client = new MongoClient(settings);
            return client.GetDatabase("<your database>");
    }
}
使用MongoDB.Bson;
使用MongoDB.Bson.Serialization;
使用MongoDB.Driver;
使用System.Collections.Generic;
使用System.Linq;
公共课程
{           
公共静态void Main()
{
var tempPipeline=新列表();
List selectFields=新建列表();
选择字段。添加(“”);
选择字段。添加(“”);
var projection=newdictionary();
//要包括字段:在项目文档中指定字段名称并设置为0。
//Ex:-排除id字段
如果(!selectFields.Contains(“\u id”))
{
添加(“_id”,0);
}
//仅返回项目文档中指定的字段。除非在项目文档中将_id字段设置为0,否则将返回该字段。
//要包括字段:指定字段名称并在项目文档中设置为1。
foreach(selectFields中的var字段)
{
添加(字段1);
//否则
//添加(字段,$“${field}”);
}
var projectStage=newbsondocument($project),projection.ToBsonDocument();
添加(projectStage.ToBsonDocument());
PipelineDefinition AggregationPipeline=tempPipeline;
var cursor=GetDatabase().GetCollection(“”).Aggregate(aggregationpipeline);
IList results=新列表();
while(cursor.MoveNext())
{
foreach(cursor.Current中的var文档)
{
添加(BsonSerializer.Deserialize(文档));
}
}
cursor.Dispose();
结果:ToList();
}
公共静态IMongoDatabase GetDatabase()
{
var设置=新的MongoClient设置
{
//设置数据库设置
};
var客户端=新的MongoClient(设置);
返回client.GetDatabase(“”);
}
}
阅读更多信息以设置返回的字段:

还有更多:

using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;

public class Program
{           
    public static void Main()
    {
            var tempPipeline = new List<BsonDocument>();

            List<string> selectFields = new List<string>();
            selectFields.Add("<your requested field 1>");
            selectFields.Add("<your requested field 2>");

            var projection = new Dictionary<string, dynamic>();

            //To include fields: Specify the field name and set to 0 in the project document.
            //Ex:- Exclue _id field
            if (!selectFields.Contains("_id"))
            {
                projection.Add("_id", 0);
            }
            //Only the fields specified in the project document are returned. The _id field is returned unless it is set to 0 in the Project document.

            //To include fields: Specify the field name and set to 1 in the project document.

            foreach (var field in selectFields)
            {
                projection.Add(field, 1);       
                //or else
                //projection.Add(field, $"${field}");
            }


            var projectStage = new BsonDocument("$project", projection.ToBsonDocument());
            tempPipeline.Add(projectStage.ToBsonDocument());

            PipelineDefinition<BsonDocument, BsonDocument> aggregatonPipeline = tempPipeline;

            var cursor =  GetDatabase().GetCollection<BsonDocument>("<your collection>").Aggregate(aggregatonPipeline);

           IList<dynamic> results = new List<dynamic>();

            while (cursor.MoveNext())
            {
                foreach (var document in cursor.Current)
                {
                    results.Add(BsonSerializer.Deserialize<dynamic>(document));
                }
            }
            cursor.Dispose();

            results.ToList();
    }

    public static IMongoDatabase GetDatabase()
        {
            var settings = new MongoClientSettings
            {
                // setup your db settings
            };

            var client = new MongoClient(settings);
            return client.GetDatabase("<your database>");
    }
}