How to deserialize a nested document into a specific class object?
I am new to MongoDb and I am trying to serialize an array of documents that are nested in a parent document in a specific class object.
The My Document object looks like this:
Project
{
"_id" : ObjectId("589da20997b2ae4608c99173"),
// ... additional fields
"Products" : [
{
"_id" : ObjectId("56971340f3a4990d2c199853"),
"ProductId" : null,
"Name" : null,
"Description" : null,
// ... quite a few more properties
"Properties" : null
}
],
"CreatorUserId" : LUUID("af955555-5555-5555-5555-f1fc4bb7cfae")
}
So basically it's a named Document Project
that contains a nested array of Product
Documents.
This is what the current classes look like:
public class Project : BaseClasses {
public ObjectId ParentCreatorUserIdProject { get; set; }
// ... additional Fields
public IEnumerable<IProjectItem> ProjectItems { get; set; }
//public IEnumerable<IProductDetails> Products { get; set; }
}
[DataContract][Serializable]
[BsonIgnoreExtraElements][MongoCollection("products")]
public class Product {
public string Name { get; set; }
public string Description { get; set;
// .. a bunch of Fields
}
Current behavior
Currently the document is being Project
returned with a serialized array Product
.
Desired behavior
I would like the document to return with a serialized array ProjectItems
. The class ProjectItems
is simple:
[Serializable]
[BsonIgnoreExtraElements]
public class ProjectItem {
public string Name { get; set; }
public string Description { get; set;
// .. a handful of Fields
}
My attempt
I tried to create a mapping using BsonClassMap
, however I just don't know enough about this type of class despite having read the entire document for display . I should note that I have defined this classmap with previous devs and other mappings and I am confident (Unit Tests) they are doing.
BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
cm.AutoMap();
cm.SetDiscriminator("ProjectItem");
cm.SetDiscriminatorIsRequired(true);
});
Unfortunately, the document is still serialized to an array Product
.
My question
How can I serialize this nested product array into an array of ProjectItem classes?
source to share
So the answer turns out to be pretty simple ... Whichever type you save it is what you want to set as your discriminator.
BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
cm.AutoMap();
cm.SetDiscriminator("Product"); // <--- it was stored as a Product
cm.SetDiscriminatorIsRequired(true);
});
Let's see another example for my MongoDb newbies ... if yours Product
was originally saved as ProductDetails
, your object would have _t
in the database. _t
tells MongoDb what type it was saved as.
So your object might look like this:
"Products" : [
{
"_t" : "ProductDetails",
"_id" : ObjectId("56971340f3a4990d2c199853"),
"ProductId" : null,
"Name" : null,
"Description" : null,
// ... quite a few more properties
"Properties" : null
}
]
Since our Product was stored with the "ProductDetail" discriminator type, we would serialize it to a ProjectItem like this:
BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
cm.AutoMap();
cm.SetDiscriminator("ProductDetails"); // <--- it was stored as a ProductDetails
cm.SetDiscriminatorIsRequired(true);
});
source to share