[英]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.