While using Mongodb.Driver I have managed to store an object in a single field. When I tried changing the object to a list of objects I ran into an error. How do I go about storing/retrieving a list of objects in Mongodb.Driver?
I am trying to write to the Transcript field (in this example). Any help is appreciated.
error message
warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100] Unhandled exception rendering component:
An error occurred while deserializing the Transcript property of class Project.Database.AllUser: Cannot deserialize
a 'List<Transcript>' from BsonType 'Document'.
example allUser document:
{
"_id" : ObjectId("63c4618d6ff75c44b29b9e04"),
"MajorID" : null,
"MinorID" : null,
"Major" : null,
"Minor" : null,
"FirstName" : "Emlynn",
"LastName" : "Gaudon",
"Birthday" : "1980-08-25",
"Email" : "EGaudon@clearwater.edu",
"Password" : "peIWoucn",
"AccountType" : "Faculty",
"Address" : "70379 Grasskamp Road",
"LoginCounter" : NumberInt(0),
"PartorFullTime" : "Part Time",
"CreditsEarned" : NumberInt(0),
"AdvisorID" : ObjectId("638ec490c57a4258b1eac559"),
"Advisor" : "None None",
"Holds" : null,
"PhoneNumber" : "262-338-0016",
"Department" : "Public Health",
"ClassCount" : NumberInt(0),
"Transcript" : {
}
}
example classes document
{
"_id" : ObjectId("637545927a45e617da1cbb90"),
"Grades" : [
],
"CRN" : NumberInt(18020),
"Attendance" : [
],
"Enrolled" : NumberInt(0),
"AttendanceDate" : [
],
"CourseName" : "Occupational Health",
"PotentialID" : "PH564",
"Section" : "003",
"Prerequiste" : [
"Introduction to Psychology",
"Research Design and Analysis I",
"Foundations of Child Development",
"English Composition I",
"English Composition II"
],
"MinimumRequirement" : [
"C",
"C",
"C",
"C",
"C"
],
"Description" : "Examines the legal, economic, and political foundations of occupational health work in the U.S. The roles of government, unions, corporations, and research organizations are examined. Students review the scientifc basis for\r\nthe association of occupational exposures with disease, including occupational cancer and respiratory disease.",
"RoomID" : NumberInt(32),
"BuildingID" : NumberInt(2),
"Type" : "Undergraduate",
"Credits" : NumberInt(4),
"MaxCapacity" : NumberInt(30),
"Day" : "Tuesday & Thursday",
"Professor" : ObjectId("63c4618d6ff75c44b29b9e04"),
"Department" : "Public Health",
"Time ID" : ObjectId("640e52efaabb89419c88139a"),
"Semester" : "Winter",
"Period" : "Period 8",
"StartDate" : ISODate("2019-01-03T01:50:00.000+0000"),
"EndDate" : ISODate("2019-01-20T03:20:00.000+0000"),
"RegistrationLimit" : ISODate("2019-01-01T00:00:00.000+0000"),
"WithdrawLimit" : ISODate("2019-01-10T00:00:00.000+0000"),
"GradeLimit" : ISODate("2019-01-22T00:00:00.000+0000"),
"ProfessorName" : "Emlynn Gaudon",
"CourseID" : NumberInt(1126),
"ProfFirstName" : "Emlynn",
"ProfLastName" : "Gaudon"
}
function.razor
@page "/function"
<button @onclick="runFunction">run function</button>
@code
{
Mongo mongo = new Mongo("database");
private void runFunction()
{
//these load the entire collection
var queryClasses = mongo.LoadRecord<Classes>("classes");
var queryUser = mongo.LoadRecord<AllUser>("allUser");
Console.WriteLine("running function");
int counter = 0;
for (int i = 0; i < queryClasses.Count; i++)
{
List<Transcript> transcript = new List<Transcript> { };
if (queryClasses[i].ProfFirstName == queryUser[counter].FirstName && queryClasses[i].ProfLastName == queryUser[counter].LastName && queryUser[counter].AccountType == "Faculty")
{
Transcript transcriptToAdd = new Transcript
{
Building = queryClasses[i].BuildingID,
CourseName = queryClasses[i].CourseName,
CourseID = queryClasses[i].CourseID,
Credits = queryClasses[i].Credits,
CRN = queryClasses[i].CRN,
EndDate = queryClasses[i].EndDate,
Grade = "C",
RoomID = queryClasses[i].RoomID,
Semester = queryClasses[i].Semester,
StartDate = queryClasses[i].StartDate
};
transcript = queryUser[counter].Transcript;
transcript.Add(transcriptToAdd);
queryUser[counter].Transcript = transcript;
mongo.UpsertRecord<AllUser>("allUser", queryUser[counter].Id, queryUser[counter]);
transcript.Clear();
}
counter++;
}
Console.WriteLine("table updated");
}
}
AllUser.cs
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Project
{
public class AllUser
{
[BsonId]
public BsonObjectId Id { get; set; }
[BsonElement("MajorID")]
public ObjectId? MajorID { get; set; } = null;
[BsonElement("MinorID")]
public ObjectId? MinorID { get; set; } = null;
[BsonElement("Major")]
public string? Major { get; set; } = null;
[BsonElement("Minor")]
public string? Minor { get; set; } = null;
[BsonElement("FirstName")]
public string? FirstName { get; set; } = null;
[BsonElement("LastName")]
public string? LastName { get; set; } = null;
[BsonElement("Birthday")]
public string? Birthday { get; set; } = null;
[BsonElement("Email")]
public string? Email { get; set; } = null;
[BsonElement("Password")]
public string? Password { get; set; } = null;
[BsonElement("AccountType")]
public string? AccountType { get; set; } = null;
[BsonElement("Address")]
public string? Address { get; set; } = null;
[BsonElement("LoginCounter")]
public int LoginCounter { get; set; } = 0;
[BsonElement("PartorFullTime")]
public string? PartOrFullTime { get; set; } = null;
[BsonElement("CreditsEarned")]
public int CreditsEarned { get; set; } = 0;
[BsonElement("AdvisorID")]
public BsonObjectId AdvisorID { get; set; }
[BsonElement("Advisor")]
public string? Advisor { get; set; }
[BsonElement("Holds")]
public string? Holds { get; set; } = null;
[BsonElement("PhoneNumber")]
public string? PhoneNumber { get; set; } = null;
[BsonElement("Department")]
public string? Department { get; set; } = null;
[BsonElement("ClassCount")]
public int ClassCount { get; set; } = 0;
[BsonElement("Transcript")]
public List<Transcript> Transcript { get; set; }
/*
[BsonElement("Schedule")]
public List<Schedule>? Schedule { get; set; }
*/
[BsonElement("StudentID")]
public string StudentID { get; set; }
}
public class Transcript
{
[BsonElement("CourseName")]
public string CourseName { get; set; }
[BsonElement("Credits")]
public int Credits { get; set; }
[BsonElement("CRN")]
public int CRN { get; set; }
[BsonElement("Semester")]
public string Semester { get; set; }
[BsonElement("Building")]
public int Building { get; set; }
[BsonElement("RoomID")]
public int RoomID { get; set; }
[BsonElement("StartDate")]
public DateTime StartDate { get; set; }
[BsonElement("EndDate")]
public DateTime EndDate { get; set; }
[BsonElement("Grade")]
public string Grade { get; set; }
[BsonElement("CourseID")]
public int CourseID { get; set; }
}
Classes.cs
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Project
{
public class Classes
{
[BsonId]
public BsonObjectId Id { get; set; }
[BsonElement("Grades")]
public BsonArray Grades { get; set; } = null;
[BsonElement("CRN")]
public int CRN {get; set; }
[BsonElement("Attendance")]
public BsonArray Attendance { get; set; } = null;
[BsonElement("Enrolled")]
public int Enrolled {get; set; } = 0;
[BsonElement("AttendanceDate")]
public BsonArray? AttendanceDate {get; set; } = null;
[BsonElement("CourseName")]
public string CourseName {get; set;} = null;
[BsonElement("PotentialID")]
public string PotentialID {get; set;} = null;
[BsonElement("Section")]
public string? Section {get; set;}
[BsonElement("Prerequiste")]
public BsonArray? Prerequiste {get; set; } = null;
[BsonElement("MinimumRequirement")]
public BsonArray? MinimumRequirements {get; set; } = null;
[BsonElement("Description")]
public string? Description {get; set; }
[BsonElement("RoomID")]
public int RoomID { get; set; }
[BsonElement("BuildingID")]
public int BuildingID { get; set; }
[BsonElement("Type")]
public string? Type {get; set;} = null;
[BsonElement("Credits")]
public int Credits {get; set; }
[BsonElement("MaxCapacity")]
public int MaxCapacity { get; set; } = 0;
[BsonElement("Day")]
public string? Day {get; set;} = null;
[BsonElement("Professor")]
public ObjectId? Professor {get; set;} = null;
[BsonElement("Department")]
public String? Department {get; set;} = null;
[BsonElement("Time ID")]
public ObjectId TimeID { get; set; }
[BsonElement("Semester")]
public string Semester { get; set; }
[BsonElement("Period")]
public string Period { get; set; }
[BsonElement("StartDate")]
public DateTime StartDate { get; set; }
[BsonElement("EndDate")]
public DateTime EndDate { get; set; }
[BsonElement("RegistrationLimit")]
public DateTime RegistrationLimit { get; set; }
[BsonElement("WithdrawLimit")]
public DateTime WithdrawLimit { get; set; }
[BsonElement("GradeLimit")]
public DateTime GradeLimit { get; set; }
[BsonElement("ProfessorName")]
public string ProfessorName { get; set; }
[BsonElement("CourseID")]
public int CourseID { get; set; }
[BsonElement("ProfFirstName")]
public string? ProfFirstName {get; set;} = null;
[BsonElement("ProfLastName")]
public string? ProfLastName {get; set;} = null;
}
}
Problem involved Transcript field (in example allUser document) being an object {} instead of a list []. I had a misconception that it had to be set as an object due to c# lists being instantiated as:
List<ClassName> listName = new List<ClassName> {};
Hope this helps somebody sometime in the future.