gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:49:15 27 4
gpt4 key购买 nike

我正在使用“MongoDB.Driver”版本=“2.9.3”。我试图只请求指定的字段。
我需要执行休耕请求(由 pypline 工作):

db.Segments.find({ "geometry" : { "$nearSphere" : { "$geometry" : { "type" : "Point", "coordinates" : [6.7889999999999997, 64.412599999999998] }, "$minDistance" : 0, "$maxDistance" : 10000 } } }, {_id : 0, 'properties.be_id' : 1,'properties.rel_ltt' : 1 ,'properties.rp_co' : 1, 'properties.wenum' : 1} ).pretty();

我需要创建 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());

这种方法似乎不起作用。
如果有人能帮我解决这个问题,我将不胜感激。

最佳答案

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

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>");
}
}

阅读更多以设置返回哪些字段: https://docs.mongodb.com/compass/master/query/project/

和更多 :

https://docs.mongodb.com/manual/crud/

https://docs.mongodb.com/manual/tutorial/query-documents/

关于c# - 如何使用 MongoDb .net 驱动程序获取集合中所有文档的几个特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com