-
Type:
Bug
-
Resolution: Done
-
Priority:
Major - P3
-
None
-
Affects Version/s: 2.2.4
-
Component/s: BSON
-
None
-
Environment:Windows10
-
None
-
None
-
None
-
None
-
None
-
None
-
None
There are following issues:
1. Cannot save struct in db as field _id gives this exception:
{BsonSerializationException: SetDocumentId cannot be used with value type UserQuery+Family.}2. struct object is stored properly when ObjectId field is removed (in my case this was missing when was storing for first time, but added later as was unable to get it back - see below)
3. BsonSerializationException: Value class UserQuery+Family cannot be deserialized. that happns when this struct object is read from db.
4. When I convert struct to class - it works fine .... but as my application is multi thread I prefer use threads to deal with false sharing on cpu cache.
linqPad code
void Main() { TestA(); TestB(); } void TestA() { var client = new MongoClient("mongodb://localhost:27017"); var database = client.GetDatabase("testError"); var collection = database.GetCollection<Family>("driverStruct"); var a = new Family{ Kids = new List<Child> { new Child{ givenName = "Child 1" + DateTime.Now.ToString(),dateOfBirth=DateTime.Now.AddDays(-2800), IsAlive=true}, new Child{ givenName = "Child 2" + DateTime.Now.ToString(),dateOfBirth=DateTime.Now.AddDays(-822), IsAlive=false} } }; collection.InsertOne(a); // throws error when ObjectId is uncommented collection.AsQueryable().ToList().Dump(); //throws error } void TestB() { var client = new MongoClient("mongodb://localhost:27017"); var database = client.GetDatabase("testError"); var collection = database.GetCollection<FamilyClass>("driverClass"); var a = new FamilyClass{ Kids = new List<ChildClass> { new ChildClass{ givenName = "Child 1" + DateTime.Now.ToString(),dateOfBirth=DateTime.Now.AddDays(-2800), IsAlive=true}, new ChildClass{ givenName = "Child 2" + DateTime.Now.ToString(),dateOfBirth=DateTime.Now.AddDays(-822), IsAlive=false} } }; collection.InsertOne(a); collection.AsQueryable().ToList().Dump(); } public struct Family{ public ObjectId _id; public List<Child> Kids; } public struct Child{ public DateTime dateOfBirth { get; set; } public string givenName { get; set; } public bool IsAlive { get; set; } } public class FamilyClass{ public ObjectId _id; public List<ChildClass> Kids; } public class ChildClass{ public DateTime dateOfBirth { get; set; } public string givenName { get; set; } public bool IsAlive { get; set; } } ================================= db.getCollection('driver').find({}) /* 1 */ { "_id" : ObjectId("57c53dd9197ecf527c31058c"), "Kids" : [ { "dateOfBirth" : ISODate("2008-12-30T09:03:37.526Z"), "givenName" : "Child 18/30/2016 9:03:37 AM", "IsAlive" : true }, { "dateOfBirth" : ISODate("2014-05-31T08:03:37.527Z"), "givenName" : "Child 28/30/2016 9:03:37 AM", "IsAlive" : false } ] } =================================================== db.getCollection('driverClass').find({}) /* 1 */ { "_id" : ObjectId("57c5412734780e1ac09c96b2"), "Kids" : [ { "dateOfBirth" : ISODate("2008-12-30T09:17:43.406Z"), "givenName" : "Child 18/30/2016 9:17:43 AM", "IsAlive" : true }, { "dateOfBirth" : ISODate("2014-05-31T08:17:43.406Z"), "givenName" : "Child 28/30/2016 9:17:43 AM", "IsAlive" : false } ] }