如何使用 MongoDb .net 驱动程序获取集合中所有文档的几个特定字段
How to get few specific fields for all documents in a collection With MongoDb .net Driver
我正在使用 "MongoDB.Driver" Version="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/
还有更多:
我正在使用 "MongoDB.Driver" Version="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/
还有更多: