使用mongodb.driver(C# .NET 7和Blazor Server)如何存储对象列表?

huangapple go评论128阅读模式
英文:

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 &#39;List&lt;Transcript&gt;&#39; from BsonType &#39;Document&#39;.

example allUser document:

{
    &quot;_id&quot; : ObjectId(&quot;63c4618d6ff75c44b29b9e04&quot;),
    &quot;MajorID&quot; : null,
    &quot;MinorID&quot; : null,
    &quot;Major&quot; : null,
    &quot;Minor&quot; : null,
    &quot;FirstName&quot; : &quot;Emlynn&quot;,
    &quot;LastName&quot; : &quot;Gaudon&quot;,
    &quot;Birthday&quot; : &quot;1980-08-25&quot;,
    &quot;Email&quot; : &quot;EGaudon@clearwater.edu&quot;,
    &quot;Password&quot; : &quot;peIWoucn&quot;,
    &quot;AccountType&quot; : &quot;Faculty&quot;,
    &quot;Address&quot; : &quot;70379 Grasskamp Road&quot;,
    &quot;LoginCounter&quot; : NumberInt(0),
    &quot;PartorFullTime&quot; : &quot;Part Time&quot;,
    &quot;CreditsEarned&quot; : NumberInt(0),
    &quot;AdvisorID&quot; : ObjectId(&quot;638ec490c57a4258b1eac559&quot;),
    &quot;Advisor&quot; : &quot;None None&quot;,
    &quot;Holds&quot; : null,
    &quot;PhoneNumber&quot; : &quot;262-338-0016&quot;,
    &quot;Department&quot; : &quot;Public Health&quot;,
    &quot;ClassCount&quot; : NumberInt(0),
    &quot;Transcript&quot; : {

    }
}

example classes document

{
    &quot;_id&quot; : ObjectId(&quot;637545927a45e617da1cbb90&quot;),
    &quot;Grades&quot; : [

    ],
    &quot;CRN&quot; : NumberInt(18020),
    &quot;Attendance&quot; : [

    ],
    &quot;Enrolled&quot; : NumberInt(0),
    &quot;AttendanceDate&quot; : [

    ],
    &quot;CourseName&quot; : &quot;Occupational Health&quot;,
    &quot;PotentialID&quot; : &quot;PH564&quot;,
    &quot;Section&quot; : &quot;003&quot;,
    &quot;Prerequiste&quot; : [
        &quot;Introduction to Psychology&quot;,
        &quot;Research Design and Analysis I&quot;,
        &quot;Foundations of Child Development&quot;,
        &quot;English Composition I&quot;,
        &quot;English Composition II&quot;
    ],
    &quot;MinimumRequirement&quot; : [
        &quot;C&quot;,
        &quot;C&quot;,
        &quot;C&quot;,
        &quot;C&quot;,
        &quot;C&quot;
    ],
    &quot;Description&quot; : &quot;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.&quot;,
    &quot;RoomID&quot; : NumberInt(32),
    &quot;BuildingID&quot; : NumberInt(2),
    &quot;Type&quot; : &quot;Undergraduate&quot;,
    &quot;Credits&quot; : NumberInt(4),
    &quot;MaxCapacity&quot; : NumberInt(30),
    &quot;Day&quot; : &quot;Tuesday &amp; Thursday&quot;,
    &quot;Professor&quot; : ObjectId(&quot;63c4618d6ff75c44b29b9e04&quot;),
    &quot;Department&quot; : &quot;Public Health&quot;,
    &quot;Time ID&quot; : ObjectId(&quot;640e52efaabb89419c88139a&quot;),
    &quot;Semester&quot; : &quot;Winter&quot;,
    &quot;Period&quot; : &quot;Period 8&quot;,
    &quot;StartDate&quot; : ISODate(&quot;2019-01-03T01:50:00.000+0000&quot;),
    &quot;EndDate&quot; : ISODate(&quot;2019-01-20T03:20:00.000+0000&quot;),
    &quot;RegistrationLimit&quot; : ISODate(&quot;2019-01-01T00:00:00.000+0000&quot;),
    &quot;WithdrawLimit&quot; : ISODate(&quot;2019-01-10T00:00:00.000+0000&quot;),
    &quot;GradeLimit&quot; : ISODate(&quot;2019-01-22T00:00:00.000+0000&quot;),
    &quot;ProfessorName&quot; : &quot;Emlynn Gaudon&quot;,
    &quot;CourseID&quot; : NumberInt(1126),
    &quot;ProfFirstName&quot; : &quot;Emlynn&quot;,
    &quot;ProfLastName&quot; : &quot;Gaudon&quot;
}

function.razor

@page &quot;/function&quot;

&lt;button @onclick=&quot;runFunction&quot;&gt;run function&lt;/button&gt;


@code
{
    Mongo mongo = new Mongo(&quot;database&quot;);

    private void runFunction()
    {
        //these load the entire collection
        var queryClasses = mongo.LoadRecord&lt;Classes&gt;(&quot;classes&quot;); 
        var queryUser = mongo.LoadRecord&lt;AllUser&gt;(&quot;allUser&quot;);


        Console.WriteLine(&quot;running function&quot;);

        int counter = 0;
        for (int i = 0; i &lt; queryClasses.Count; i++)
        {
            List&lt;Transcript&gt; transcript = new List&lt;Transcript&gt; { };

            if (queryClasses[i].ProfFirstName == queryUser
0
+
网站访问量
.FirstName &amp;&amp; queryClasses[i].ProfLastName == queryUser
0
+
网站访问量
.LastName &amp;&amp; queryUser
0
+
网站访问量
.AccountType == &quot;Faculty&quot;) { 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 = &quot;C&quot;, RoomID = queryClasses[i].RoomID, Semester = queryClasses[i].Semester, StartDate = queryClasses[i].StartDate }; transcript = queryUser
0
+
网站访问量
.Transcript; transcript.Add(transcriptToAdd); queryUser
0
+
网站访问量
.Transcript = transcript; mongo.UpsertRecord&lt;AllUser&gt;(&quot;allUser&quot;, queryUser
0
+
网站访问量
.Id, queryUser
0
+
网站访问量
); transcript.Clear(); } counter++; } Console.WriteLine(&quot;table updated&quot;); } }

AllUser.cs

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace Project
{
    public class AllUser
    {
        [BsonId]
        public BsonObjectId Id { get; set; }

        [BsonElement(&quot;MajorID&quot;)]
        public ObjectId? MajorID { get; set; } = null;

        [BsonElement(&quot;MinorID&quot;)]
        public ObjectId? MinorID { get; set; } = null;

        [BsonElement(&quot;Major&quot;)]
        public string? Major { get; set; } = null;

        [BsonElement(&quot;Minor&quot;)]
        public string? Minor { get; set; } = null;

        [BsonElement(&quot;FirstName&quot;)]
        public string? FirstName { get; set; } = null;

        [BsonElement(&quot;LastName&quot;)]
        public string? LastName { get; set; } = null;

        [BsonElement(&quot;Birthday&quot;)]
        public string? Birthday { get; set; } = null;

        [BsonElement(&quot;Email&quot;)]
        public string? Email { get; set; } = null;

        [BsonElement(&quot;Password&quot;)]
        public string? Password { get; set; } = null;

        [BsonElement(&quot;AccountType&quot;)]
        public string? AccountType { get; set; } = null;

        [BsonElement(&quot;Address&quot;)]
        public string? Address { get; set; } = null;

        [BsonElement(&quot;LoginCounter&quot;)]
        public int LoginCounter { get; set; } = 0;

        [BsonElement(&quot;PartorFullTime&quot;)]
        public string? PartOrFullTime { get; set; } = null;

        [BsonElement(&quot;CreditsEarned&quot;)]
        public int CreditsEarned { get; set; } = 0;

        [BsonElement(&quot;AdvisorID&quot;)]
        public BsonObjectId AdvisorID { get; set; }

        [BsonElement(&quot;Advisor&quot;)]
        public string? Advisor { get; set; }

        [BsonElement(&quot;Holds&quot;)]
        public string? Holds { get; set; } = null;

        [BsonElement(&quot;PhoneNumber&quot;)]
        public string? PhoneNumber { get; set; } = null;

        [BsonElement(&quot;Department&quot;)]
        public string? Department { get; set; } = null;

        [BsonElement(&quot;ClassCount&quot;)]
        public int ClassCount { get; set; } = 0;

        [BsonElement(&quot;Transcript&quot;)]
        public List&lt;Transcript&gt; Transcript { get; set; }

        /*
        [BsonElement(&quot;Schedule&quot;)]
        public List&lt;Schedule&gt;? Schedule { get; set; }
        */
        [BsonElement(&quot;StudentID&quot;)]
        public string StudentID { get; set; }
    }


    public class Transcript
    {
        [BsonElement(&quot;CourseName&quot;)]
        public string CourseName { get; set; }

        [BsonElement(&quot;Credits&quot;)]
        public int Credits { get; set; }

        [BsonElement(&quot;CRN&quot;)]
        public int CRN { get; set; }

        [BsonElement(&quot;Semester&quot;)]
        public string Semester { get; set; }

        [BsonElement(&quot;Building&quot;)]
        public int Building { get; set; }

        [BsonElement(&quot;RoomID&quot;)]
        public int RoomID { get; set; }

        [BsonElement(&quot;StartDate&quot;)]
        public DateTime StartDate { get; set; }

        [BsonElement(&quot;EndDate&quot;)]
        public DateTime EndDate { get; set; }

        [BsonElement(&quot;Grade&quot;)]
        public string Grade { get; set; }

        [BsonElement(&quot;CourseID&quot;)]
        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(&quot;Grades&quot;)]
        public BsonArray Grades { get; set; } = null;

        [BsonElement(&quot;CRN&quot;)]
        public int CRN {get; set; } 

        [BsonElement(&quot;Attendance&quot;)]
        public BsonArray Attendance { get; set; } = null;

        [BsonElement(&quot;Enrolled&quot;)]
        public int Enrolled {get; set; } = 0;

        [BsonElement(&quot;AttendanceDate&quot;)]
        public BsonArray? AttendanceDate {get; set; } = null;

        [BsonElement(&quot;CourseName&quot;)]
        public string CourseName {get; set;} = null;

        [BsonElement(&quot;PotentialID&quot;)]
        public string PotentialID {get; set;} = null;

        [BsonElement(&quot;Section&quot;)]
        public string? Section {get; set;} 

        [BsonElement(&quot;Prerequiste&quot;)]
        public BsonArray? Prerequiste {get; set; } = null;

        [BsonElement(&quot;MinimumRequirement&quot;)]
        public BsonArray? MinimumRequirements {get; set; } = null;

        [BsonElement(&quot;Description&quot;)]
        public string? Description {get; set; }

        [BsonElement(&quot;RoomID&quot;)]
        public int RoomID { get; set; }

        [BsonElement(&quot;BuildingID&quot;)]
        public int BuildingID { get; set; }

        [BsonElement(&quot;Type&quot;)]
        public string? Type {get; set;} = null;

        [BsonElement(&quot;Credits&quot;)]
        public int Credits {get; set; }

        [BsonElement(&quot;MaxCapacity&quot;)]
        public int MaxCapacity { get; set; } = 0;

        [BsonElement(&quot;Day&quot;)]
        public string? Day {get; set;} = null;

        [BsonElement(&quot;Professor&quot;)]
        public ObjectId? Professor {get; set;} = null;

        [BsonElement(&quot;Department&quot;)]
        public String? Department {get; set;} = null;

        [BsonElement(&quot;Time ID&quot;)]
        public ObjectId TimeID { get; set; }

        [BsonElement(&quot;Semester&quot;)]
        public string Semester { get; set; }

        [BsonElement(&quot;Period&quot;)]
        public string Period { get; set; }

        [BsonElement(&quot;StartDate&quot;)]
        public DateTime StartDate { get; set; }

        [BsonElement(&quot;EndDate&quot;)]
        public DateTime EndDate { get; set; }

        [BsonElement(&quot;RegistrationLimit&quot;)]
        public DateTime RegistrationLimit { get; set; }

        [BsonElement(&quot;WithdrawLimit&quot;)]
        public DateTime WithdrawLimit { get; set; }

        [BsonElement(&quot;GradeLimit&quot;)]
        public DateTime GradeLimit { get; set; }

        [BsonElement(&quot;ProfessorName&quot;)]
        public string ProfessorName { get; set; }

        [BsonElement(&quot;CourseID&quot;)]
        public int CourseID { get; set; }

        [BsonElement(&quot;ProfFirstName&quot;)] 
        public string? ProfFirstName {get; set;} = null; 
        
        [BsonElement(&quot;ProfLastName&quot;)]
        public string? ProfLastName {get; set;} = null;

    }
}

答案1

得分: 0

涉及的问题是转录字段(例如所有用户文档中)是一个对象 {},而不是一个列表 []。我误以为它必须设置为一个对象,因为在 C# 中,列表被实例化为:

List&lt;ClassName&gt; listName = new List&lt;ClassName&gt; {};

希望这对将来的某人有所帮助。

英文:

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&lt;ClassName&gt; listName = new List&lt;ClassName&gt; {};

Hope this helps somebody sometime in the future.

huangapple
  • 本文由 发表于 2023年4月4日 04:25:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定