I am trying to write a method which would return all the Book documents from the MongoDB to my mvc application. First, I connect to database, retrieve collection and convert that collection into Json file. Next I create a list which has couple fields specified (name, author, etc.) using serializer i try to deserialize it into list and using a for loop return the list of books. Sadly I get error in the return line (convertion error). Any suggests are welcomed!
public List<Book> getAllBooks()
{
var mongoClient = new MongoClient("mongodb://localhost");
var database = mongoClient.GetDatabase("SearchForKnowledge");
var coll = database.GetCollection<BsonDocument>("Book");
coll.ToJson();
List<Book> collection = new List<Book>();
JavaScriptSerializer js = new JavaScriptSerializer();
collection = (List<Book>)Newtonsoft.Json.JsonConvert.DeserializeObject(coll.ToString());
for (int i = 0; i < collection.Count(); i++)
{
return collection[i];
}
}