简体   繁体   English

无法反序列化“列表”<Object> &#39; 来自 BsonType &#39;文档&#39;

[英]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:我在一个类中声明了一个List<object>来声明属性,并使用 C# 从 mongodb 中获取数据,例如:

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

but it throws an error:但它抛出一个错误:

"Cannot deserialize a 'List' from BsonType 'Document'" “无法从 BsonType 'Document' 反序列化一个 'List'”

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.在 Mongo DB 中,对于集合中的每个条目,都会有对象 id 后跟字段。 To import those data, you need to declare a class which is having same mongo DB fields, like要导入这些数据,您需要声明一个具有相同 mongo DB 字段的类,例如

// 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所以从datatbase获取所有数据的最终代码如下

           //  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.确保您的 Mongo 服务器运行正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 BsonType“Array”反序列化“BsonDocument” - Cannot deserialize a 'BsonDocument' from BsonType 'Array' 无法从BsonType&#39;Array&#39;反序列化&#39;BsonDocument - Cannot deserialize a 'BsonDocument from BsonType 'Array' 无法从 BsonType &#39;ObjectId&#39; 反序列化 &#39;String&#39; - Cannot deserialize a 'String' from BsonType 'ObjectId' FormatException:无法从 BsonType 'ObjectId' 反序列化 'Guid' - FormatException: Cannot deserialize a 'Guid' from BsonType 'ObjectId' C#Mongo驱动程序错误:无法从BsonType&#39;Document&#39;反序列化&#39;Int32&#39; - C# Mongo Driver error: Cannot deserialize a 'Int32' from BsonType 'Document' MongoDB C#2驱动程序—无法从BsonType&#39;Double&#39;反序列化&#39;String&#39; - MongoDB C# 2 Driver — Cannot deserialize 'String' from BsonType 'Double' 无法从MongoDb C#中的BsonType ObjectId反序列化字符串# - Cannot deserialize string from BsonType ObjectId in MongoDb C# c#和mongoDB-无法从BsonType Null反序列化 - c# and mongoDB - Cannot deserialize from BsonType Null MongoDB C#:无法从BsonType&#39;DateTime&#39;反序列化&#39;String&#39; - MongoDB C#: Cannot deserialize a 'String' from BsonType 'DateTime' .NET MongoDB:从BsonType数组反序列化BsonDocument - .NET MongoDB: Deserialize BsonDocument from BsonType Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM