[英]Using C# to Retrieve a MongoDB Document and Select One Field
我在ASP.NET Core中使用C#從MongoDB Atlas集合中檢索隨機文檔,提取特定字段(“名稱”)並將其添加到Web服務響應中。
由於這對我來說是第一次,所以我研究了幾種解決方案,其中大多數是在這些論壇上提出的,但該領域尚未得到解決。
這是怎么回事。
(請注意,出於本文的目的,已替換了數據庫的詳細信息。)
1)在創建用於提取字段的方法中,我在定義投影並返回結果之前檢索了隨機文檔。
var client = new MongoClient("CLIENT");
var database = client.GetDatabase("DATABASE");
var collection = database.GetCollection<BsonDocument>("COLLECTION");
var document = new BsonDocument(collection.AsQueryable().Sample(1).FirstOrDefault());
var projection = Builders<BsonDocument>.Projection.Include("name").Exclude("_id");
var result = collection.Find<BsonDocument>(document).Project(projection).ToString();
return result.name;
但是,結果如下。
{“ city”:“ find({\\” _ id \\“:ObjectId(\\” 5c4f2f7b9914ed3a4b8aa075 \\“),\\” id \\“:3698105,\\” coord \\“:{\\” lon \\“:-80.162497999999999,\\” lat \\“:-5.0925000000000002},\\” country \\“:\\” PE \\“,\\” geoname \\“:{\\” cl \\“:\\” P \\“,\\” code \\“:\\” PPL \\“ ,\\“ parent \\”:3693525},\\“ name \\”:\\“ Chulucanas \\”,\\“ stat \\”:{\\“ level \\”:1.0,\\“人口\\”:68835},\\“ zoom \\ “:9},{\\”名稱\\“:1,\\” _ id \\“:0}
2)因此,我嘗試解決此問題。 我省略了投影,並嘗試將文檔轉換為JSON,將其映射到數據模型並選擇所需的字符串。
資料模型
public class Document
{
public string _id { get; set; }
public int id { get; set; }
public string coord { get; set; }
public string country { get; set; }
public string geoname { get; set; }
public string langs { get; set; }
public string name { get; set; }
public string stat { get; set; }
public string stations { get; set; }
public int zoom { get; set; }
}
方法
var client = new MongoClient("CLIENT");
var database = client.GetDatabase("DATABASE");
var collection = database.GetCollection<BsonDocument>("COLLECTION");
var document = new BsonDocument(collection.AsQueryable().Sample(1).FirstOrDefault()).ToJson();
Document result = JsonConvert.DeserializeObject<Document>(document);
return result.name;
結果是一個空白屏幕和一個Newtonsoft.Json> JsonReaderException。
為了調試,我更換了
return result.name
同
return document;
並注釋掉以下行。
Document result = JsonConvert.DeserializeObject<Document>(document);
再次,結果是相似的。
“ city”:“ {\\” _ id \\“:ObjectId(\\” 5c4f2f839914ed3a4b8aa95f \\“),\\” id \\“:4900579,\\” coord \\“:{\\” lon \\“:-89.058159000000003,\\” lat \\“ :42.320019000000002},\\“ country \\”:\\“ US \\”,\\“ geoname \\”:{\\“ cl \\”:\\“ P \\”,\\“ code \\”:\\“ PPL \\”,\\“父級\\“:4916845},\\” langs \\“:[{\\” link \\“:\\” http://en.wikipedia.org/wiki/Loves_Park%2C_Illinois \\“},{\\” post \\“:\\” 61111 \\“}],\\”名稱\\“:\\”愛情公園\\“,\\” stat \\“:{\\”級別\\“:1.0,\\”人口\\“:23996},\\”站點\\“:[ {\\“ id \\”:974,\\“ dist \\”:43,\\“ kf \\”:1},{\\“ id \\”:1005,\\“ dist \\”:13,\\“ kf \\”:1 },{\\“ id \\”:2989,\\“ dist \\”:33,\\“ kf \\”:1},{\\“ id \\”:3010,\\“ dist \\”:24,\\“ kf \\” :1},{\\“ id \\”:9277,\\“ dist \\”:35,\\“ kf \\”:1},{\\“ id \\”:27762,\\“ dist \\”:16,\\“ kf \\“:1},{\\” id \\“:29682,\\” dist \\“:49,\\” kf \\“:1},{\\” id \\“:30211,\\” dist \\“:45,\\ “ kf \\”:1},{\\“ id \\”:31051,\\“ dist \\”:42,\\“ kf \\”:1},{\\“ id \\”:31756,\\“ dist \\”:15 ,\\“ kf \\”:1},{\\“ id \\”:32614,\\“ dist \\”:49,\\“ kf \\”:1},{\\“ id \\”:32643,\\“ dis t \\“:47,\\” kf \\“:1},{\\” id \\“:33291,\\” dist \\“:12,12,\\” kf \\“:1},{\\” id \\“:33712, \\“ dist \\”:36,\\“ kf \\”:1},{\\“ id \\”:33902,\\“ dist \\”:38,\\“ kf \\”:1},{\\“ id \\”: 33980,\\“ dist \\”:42,\\“ kf \\”:1},{\\“ id \\”:34925,\\“ dist \\”:10,\\“ kf \\”:1}],\\“縮放\\ :: 12}
在此先感謝您的建議。
在第一次嘗試(動態方法)中,您混合使用Linq版本,而不是將結果與混合的.Find
版本一起使用。 您可以使用完整的Linq方法>
private string GetName()
{
var client = new MongoClient();
var database = client.GetDatabase("WorldCities");
var collection = database.GetCollection<BsonDocument>("cities");
return collection.AsQueryable().Sample(1).First().GetValue("name").ToString();
}
還是完整的。 .Find
方法>
private string GetName()
{
var client = new MongoClient();
var database = client.GetDatabase("WorldCities");
var collection = database.GetCollection<BsonDocument>("cities");
var result = collection.Find(FilterDefinition<BsonDocument>.Empty)
.Project(Builders<BsonDocument>.Projection.Include("name").Exclude("_id")).First().ToString();
return result;
// { "name" : "les Escaldes" }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.