Been trying to fetch documents from a collection, and the documents contains nested documents.
Getting an error message saying “Expected a nested document representing the serialized form of a Project.Models.MapTags value, but found a value of String instead”
Here is the code
This is the Method getting the collection and does the query.
public List<Maps> GetAllMapsByUserId(ObjectId userId)
{
using (_server.RequestStart(_db))
{
var query = Query.EQ("UserId", userId);
MongoCursor<Maps> maps = _db.GetCollection<Maps>"Maps").FindAs<Maps>(query);
var list = maps.ToList();
return list;
}
}
This is the class which contains the object
namespace Project.Models
{
public class Maps {
[BsonId]
public ObjectId Id { get; set; }
public ObjectId UserId { get; set; }
public string MapName { get; set; }
public string Description { get; set; }
public BsonBoolean PublicMap { get; set; }
public DateTime Created { get; set; }
[BsonIgnoreIfNull]
public List<MapTags> Tags { get; set; }
[BsonIgnoreIfNull]
public List<MapVotes> Votes { get; set; }
}
public class MapVotes
{
public ObjectId VoterId { get; set; }
public int VoteValue { get; set; }
}
public class MapTags
{
public string Tag { get; set; }
}
}
Hope that someone has an idea/tip or similar, been searching and testing for a long time and haven’t found a solution.
Update :
Here is an example of data in the database
{
"_id" : ObjectId("4eda2415851e702684bf6392"),
"MapName" : "Test",
"Description" : "Test",
"Created" : ISODate("2011-12-03T13:28:53.698Z"),
"PublicMap" : false,
"UserId" : ObjectId("4e8033a0851e701c7c1e12e1"),
"Tags" : [ "Test", "Kalle", "Jonas", "Fredrik" ]
}
It looks like the serializer expects a document, but finds a string.
The problem is that you have serialized an array of strings:
but you should have
The deserializer expects to find a document, but finds only a string. Now it’s possible that the
MapTagclass can be essentially serialized using a string and can be reconstructed from a string, but you have to provide a custom serializer for that. It’s pretty straightforward.