英文:
Using mongodb.driver (c# .net7 and blazor server) how do you store a list of objects?
问题
在使用 Mongodb.Driver 时,我成功地将一个对象存储在单个字段中。当我尝试将该对象更改为对象列表时,遇到了错误。我该如何在 Mongodb.Driver 中存储/检索对象列表?
错误信息:
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'.
示例中的 allUser 文档:
{
"_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" : 0,
"PartorFullTime" : "Part Time",
"CreditsEarned" : 0,
"AdvisorID" : ObjectId("638ec490c57a4258b1eac559"),
"Advisor" : "None None",
"Holds" : null,
"PhoneNumber" : "262-338-0016",
"Department" : "Public Health",
"ClassCount" : 0,
"Transcript" : [
]
}
示例中的 classes 文档:
{
"_id" : ObjectId("637545927a45e617da1cbb90"),
"Grades" : [
],
"CRN" : 18020,
"Attendance" : [
],
"Enrolled" : 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" : 32,
"BuildingID" : 2,
"Type" : "Undergraduate",
"Credits" : 4,
"MaxCapacity" : 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" : 1126,
"ProfFirstName" : "Emlynn",
"ProfLastName" : "Gaudon"
}
在 function.razor 文件中,您尝试运行一个函数,该函数加载了整个集合,并尝试更新表中的数据。
在 AllUser.cs 文件中,您定义了 AllUser 类和 Transcript 类,其中 Transcript 类包含了课程相关的信息。
在 Classes.cs 文件中,您定义了 Classes 类,其中包含了课程的详细信息。
请问您有什么需要进一步了解或需要帮助的地方吗?
英文:
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 == queryUser0+网站访问量.FirstName && queryClasses[i].ProfLastName == queryUser0+网站访问量.LastName && queryUser0+网站访问量.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 = queryUser0+网站访问量.Transcript;
transcript.Add(transcriptToAdd);
queryUser0+网站访问量.Transcript = transcript;
mongo.UpsertRecord<AllUser>("allUser", queryUser0+网站访问量.Id, queryUser0+网站访问量);
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;
}
}
答案1
得分: 0
涉及的问题是转录字段(例如所有用户文档中)是一个对象 {},而不是一个列表 []。我误以为它必须设置为一个对象,因为在 C# 中,列表被实例化为:
List<ClassName> listName = new List<ClassName> {};
希望这对将来的某人有所帮助。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论