首页 > 解决方案 > 如何在以下 JSON 值中仅检索状态真或假

问题描述

        Console.WriteLine("mongodb localhost connecting...");
        var client = new MongoClient("mongodb://localhost");
        var db = client.GetDatabase("mobtions");                  

        var collection = db.GetCollection<BsonDocument>("AffiliTestStatus");
        var filter = new Dictionary<string, string>();
        filter.Add("offerid", "102030");

        dynamic records = collection.Find(new BsonDocument(filter)).ToList();

这是我从上面的代码中读到的 JSON 响应

{{ "_id" : ObjectId("5e411eca5b7dfc53ac571f71"), "offerid" : "102030", "status" : true }}

标签: jsonmongodbc#-4.0

解决方案


试试这个:

    var filter = Builders<BsonDocument>.Filter.Eq("offerid", "102030");
    var projection = Builders<BsonDocument>.Projection.Include("status").Exclude("_id");
    var options = new FindOptions<BsonDocument> { Projection = projection };
    var records = collection.FindSync(filter, options).ToList();

推荐阅读