Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserialization Error ASP.NET MongoDB
    primarykey
    data
    text
    <p>I am trying to retireve all the documents in my MongoDB collection via a REST api built in ASP.NET MVC 4 and I have encoutered an error when i enter localhost:50491/api/document:</p> <blockquote> <p>An error occurred while deserializing the Id property of class Acord_Rest_API.Models.Document: Cannot deserialize string from BsonType ObjectId.</p> </blockquote> <p>My controller looks like:</p> <pre><code>public class DocumentController : ApiController { public readonly MongoConnectionHelper&lt;Document&gt; docs; public DocumentController() { docs = new MongoConnectionHelper&lt;Document&gt;(); } public IList&lt;Document&gt; getAllDocs() { var alldocs = docs.collection.FindAll(); return alldocs.ToList(); } } </code></pre> <p>My MongoDB document looks like this? <img src="https://i.stack.imgur.com/x627q.png" alt="enter image description here"></p> <p>What am I missing here?</p> <p>My class that connects to the DB:</p> <pre><code>public class MongoConnectionHelper&lt;T&gt; where T: class { public MongoCollection&lt;T&gt; collection { get; private set; } public MongoConnectionHelper() { string connectionString = "mongodb://127.0.0.1"; var server = MongoServer.Create(connectionString); if (server.State == MongoServerState.Disconnected) { server.Connect(); } var conn = server.GetDatabase("Acord"); collection = conn.GetCollection&lt;T&gt;("Mappings"); } } </code></pre> <p>EDIT here is my solution: <img src="https://i.stack.imgur.com/pWmum.png" alt="enter image description here"></p> <p>MongoConnectionHelper does the connection to the DB, DocumentController has the methods to retrive all the documents and Document contains what you have suggested in your answer.</p> <p>EDIT here is the Document class:</p> <pre><code>[DataContract] public class Document { public ObjectId _id { get; set; } [DataMember] public string MongoId { get { return _id.ToString(); } set { _id = ObjectId.Parse(value); } } public IList&lt;string&gt; alldocs { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload