简体   繁体   中英

How to fetch a list of values from a Bson document?

I'm parsing in a Json document that stores a list of type <Country> (which holds the name and code ), using the MongoDB.Net driver . But I'm not sure how to retrieve that list of countries from the Bson document.

I stepped through the parsing code, all values are being parsed in to the CountryModel. Then stored in the returned collection var.

Googling brought me this solution but it only shows how to return a record by ID not a complete List. I'm wondering if a Lambda overload can be used here to find the list.

So far I've managed to retrieve the complete doc, and assign it to a list :

countries = collection.Find(new BsonDocument()).ToListAsync().GetAwaiter().GetResult();

Does anyone know how I can fetch just List<Country> and not the whole document?

The two main methods involved in retrieving the data are as follows:

    public void LoadDb()
    {
        var collection = StartConnection();          
        countries = collection.Find(new BsonDocument()).ToListAsync().GetAwaiter().GetResult();

    }


    public IMongoCollection<CountryModel> StartConnection()
    {            
        var client = new MongoClient(connectionString);
        var database = client.GetDatabase("orders");
        //Get a handle on the countries collection:
        var collection = database.GetCollection<CountryModel>("countries");
        return collection;
    }

This is a sample of the Json data that's parsed in:

{
    "_id": {
        "$oid": "565f5d3ae4b0ed465284d17a"
    },
    "countries": [
        {
            "name": "Afghanistan",
            "code": "AF"
        },
        {
            "name": "Antigua and Barbuda",
            "code": "AG"
        }
    ]
}

And this is the backing POCO class, CountryModel :

namespace MongoDBApp.Models
{
    [ImplementPropertyChanged]
    public class CountryModel
    {

        [BsonId]
        public ObjectId Id { get; set; }

        [BsonElement("countries")]
        public List<Country> countries { get; set; }



    }

    [ImplementPropertyChanged]
    public class Country
    {
        [BsonElement("name")]
        public string Name { get; set; }

        [BsonElement("code")]
        public string Code { get; set; }
    }
}

Try with projection:

 var result = await collection.Find(x => x.Id == {ObjectId}).Project(x => x.countries).FirstOrDefaultAsync();

Where {ObjectId} is the id of the CountryModel from which you want to fetch the countries collection.

Btw: There is more convenient way for using ObjectId . You can place string in your model and add attributes:

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }

Then you can use Find with Id in string :

 collection.Find(x => x.Id == "sample_object_id")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM