简体   繁体   中英

Cannot deserialize a 'List<Object>' from BsonType 'Document'

I have declared a List<object> in a class for declaring property, and fetching data from mongodb using C#, ex:

public List<object> name {get;set;} 

but it throws an error:

"Cannot deserialize a 'List' from BsonType 'Document'"

Your question is not clear, hope this answer can help you. In Mongo DB, for every entry in a collection, there will be object id followed by fields. To import those data, you need to declare a class which is having same mongo DB fields, like

// using MongoDB.Bson;
public class ToDo
    {
        public ObjectId Id { get; set; }
        public long ID { get; set; }
        public string Title { get; set; }
        public string Status { get; set; }
    }

so final code to get all datat from datatbase like follows

           //  using MongoDB.Driver;
           //  using MongoDB.Bson;
             private static Object GetCollection()
              {
                IMongoClient  _client = new MongoClient();
                IMongoDatabase _database = _client.GetDatabase("<urDBname>");
                var _collection = _database.GetCollection<ToDo>("<urCOLLECTIONname>");
                var documents = _collection.Find(new BsonDocument()).ToListAsync().Result;
                return documents;
              }

here documents will give the list of documents present in database collections. Make sure that your Mongo server is running properly.

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