使用C#检索MongoDB文档并选择一个字段

时间:2019-01-30 09:17:08

标签: c# asp.net mongodb

我在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”:“查找({\” _ id \“:ObjectId(\” 5c4f2f7b9914ed3a4b8aa075 \“),   \“ id \”:3698105,\“ coord \”:{\“ lon \”:-80.162497999999999,\“ lat \”   :-5.0925000000000002},\“ country \”:\“ PE \”,\“ geoname \”:{\“ cl \”   :\“ P \”,\“代码\”:\“ PPL \”,\“父母\”:3693525},\“名称\”:   \“ 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 \”,\“ parent \ “:4916845},\” langs \“:[{   \“ link \”:\“ http://en.wikipedia.org/wiki/Loves_Park%2C_Illinois \”},   {\“ post \”:\“ 61111 \”}],\“ name \”:\“ Loves Park \”,\“ stat \”:{   \“ level \”:1.0,\“人口\”:23996},\“ stations \”:[{\“ 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,\“ dist \”:47,\“ kf \”:1},{\“ id \”   :33291,\“ dist \”: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}],\“ zoom \”:12}

预先感谢您的建议。

1 个答案:

答案 0 :(得分:1)

在第一次尝试(动态方法)中,您混合使用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" }
}