Simple.MongoDB.Relation 1.0.1

dotnet add package Simple.MongoDB.Relation --version 1.0.1
NuGet\Install-Package Simple.MongoDB.Relation -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Simple.MongoDB.Relation" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Simple.MongoDB.Relation --version 1.0.1
#r "nuget: Simple.MongoDB.Relation, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Simple.MongoDB.Relation as a Cake Addin
#addin nuget:?package=Simple.MongoDB.Relation&version=1.0.1

// Install Simple.MongoDB.Relation as a Cake Tool
#tool nuget:?package=Simple.MongoDB.Relation&version=1.0.1

Simple.MongoDB.Relation

You can create simple (one level deep) relations with attributes

Installing

You can install package directly from nuget.org

Install-Package Simple.MongoDB.Relation -Version 1.0.1

Configuration

You can decorate your model properties with MongoRelation attirube.

Definition of Attribute Properties

FromCollection: Name of principal (remote) collection
LocalFieldId: Name of local property which will be used for relation.
PrincipalValueField: If relation property is a primitive type. This field will be taken from principal entity and will be setted to our relation field. Default value is "Name".
PrincipalFieldId: Principal collection Id field, which will be matched with LocalFieldId. Default value is "_id".

Model Configurations

IMPORTANT: You must decorate relation properties with BsonIgnoreIfDefault attribute.

    public class Student
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }

        public string Name { get; set; }

        [BsonRepresentation(BsonType.ObjectId)]
        public string ClassId { get; set; }

        public int GenderId { get; set; }

        [MongoRelation(FromCollection = "Genders", LocalFieldId = nameof(GenderId), PrincipalValueField = "Text", PrincipalFieldId = "_id")]
        [BsonIgnoreIfDefault]
        public string GenderText { get; set; }

        [MongoRelation(FromCollection = "Classes", LocalFieldId = nameof(ClassId))]
        [BsonIgnoreIfDefault]
        public Class StudentClass { get; set; }
    }

Explanation of relation at GenderText: Relation will use GenderId in our class, and look for _id field at Genders collection. If a matched record found, it will get Text property in matched record and set the value to our GenderText field.

Explanation of relation at StudentClass: Relation will use ClassId in our class, and look for _id field at Classes collection. If a matched record found, it will get matched record and set the record to our StudentClass field.

    public class Class
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }

        public string Name { get; set; }

        [BsonRepresentation(BsonType.ObjectId)]
        [BsonIgnoreIfNull]
        public List<string> StudentIds { get; set; }

        [MongoRelation(FromCollection = "Students", LocalFieldId = nameof(StudentIds))]
        [BsonIgnoreIfDefault]
        public List<Student> Students { get; set; }
    }

Explanation of relation at Students: Relation will use StudentIds in our class, and look for _id field at Students collection. If matched records found, it will get matched records and set the records to our Students field.

    public class Gender
    {
        [BsonId]
        public int Id { get; set; }

        public string Text { get; set; }
    }

Usage

Import Reference

using Simple.MongoDB.Relation;
    var client = new MongoClient("yourconnectionstring");
    var database = client.GetDatabase("test");

    var classesCollection = database.GetCollection<Class>("Classes");
    var studentsCollection = database.GetCollection<Student>("Students");

    if (!classesCollection.Find(x => true).ToList().Any())
    {
        var classOne = new Class()
        {
            Id = ObjectId.GenerateNewId().ToString(),
            Name = "Class One"
        };

        classesCollection.InsertOne(classOne);

        var genderMale = new Gender()
        {
            Id = 1,
            Text = "Male"
        };

        var genderFemale = new Gender()
        {
            Id = 2,
            Text = "Female"
        };

        gendersCollection.InsertOne(genderMale);
        gendersCollection.InsertOne(genderFemale);


        var student1 = new Student()
        {
            Id = ObjectId.GenerateNewId().ToString(),
            ClassId = classOne.Id,
            GenderId = genderMale.Id,
            Name = "Barrack Obama"
        };

        var student2 = new Student()
        {
            Id = ObjectId.GenerateNewId().ToString(),
            ClassId = classOne.Id,
            GenderId = genderMale.Id,
            Name = "Vladimir Putin"
        };

        var student3 = new Student()
        {
            Id = ObjectId.GenerateNewId().ToString(),
            ClassId = classOne.Id,
            GenderId = genderFemale.Id,
            Name = "Hillary Clinton"
        };

        studentsCollection.InsertOne(student1);
        studentsCollection.InsertOne(student2);
        studentsCollection.InsertOne(student3);


        classOne.StudentIds = new List<string>()
        {
            student1.Id, student2.Id, student3.Id
        };

        classesCollection.ReplaceOne(x => x.Id == classOne.Id, classOne);
    }

    var students = studentsCollection.FindWithRelations();
    var classes = classesCollection.FindWithRelations();

Results

Result for return Json(students.ToList());

  [
   {
      "id":"59fe19d9f05d8c237858b96e",
      "name":"Barrack Obama",
      "classId":"59fe19d6f05d8c237858b96d",
      "genderId":1,
      "genderText":"Male",
      "studentClass":{
         "id":"59fe19d6f05d8c237858b96d",
         "name":"Class One",
         "studentIds":[
            "59fe19d9f05d8c237858b96e",
            "59fe19d9f05d8c237858b96f",
            "59fe19d9f05d8c237858b970"
         ],
         "students":null
      }
   },
   {
      "id":"59fe19d9f05d8c237858b96f",
      "name":"Vladimir Putin",
      "classId":"59fe19d6f05d8c237858b96d",
      "genderId":1,
      "genderText":"Male",
      "studentClass":{
         "id":"59fe19d6f05d8c237858b96d",
         "name":"Class One",
         "studentIds":[
            "59fe19d9f05d8c237858b96e",
            "59fe19d9f05d8c237858b96f",
            "59fe19d9f05d8c237858b970"
         ],
         "students":null
      }
   },
   {
      "id":"59fe19d9f05d8c237858b970",
      "name":"Hillary Clinton",
      "classId":"59fe19d6f05d8c237858b96d",
      "genderId":2,
      "genderText":"Female",
      "studentClass":{
         "id":"59fe19d6f05d8c237858b96d",
         "name":"Class One",
         "studentIds":[
            "59fe19d9f05d8c237858b96e",
            "59fe19d9f05d8c237858b96f",
            "59fe19d9f05d8c237858b970"
         ],
         "students":null
      }
   }
  ]

Result for return Json(classes.ToList());

  [
   {
      "id":"59fe19d6f05d8c237858b96d",
      "name":"Class One",
      "studentIds":[
         "59fe19d9f05d8c237858b96e",
         "59fe19d9f05d8c237858b96f",
         "59fe19d9f05d8c237858b970"
      ],
      "students":[
         {
            "id":"59fe19d9f05d8c237858b96e",
            "name":"Barrack Obama",
            "classId":"59fe19d6f05d8c237858b96d",
            "genderId":1,
            "genderText":null,
            "studentClass":null
         },
         {
            "id":"59fe19d9f05d8c237858b96f",
            "name":"Vladimir Putin",
            "classId":"59fe19d6f05d8c237858b96d",
            "genderId":1,
            "genderText":null,
            "studentClass":null
         },
         {
            "id":"59fe19d9f05d8c237858b970",
            "name":"Hillary Clinton",
            "classId":"59fe19d6f05d8c237858b96d",
            "genderId":2,
            "genderText":null,
            "studentClass":null
         }
      ]
   }
  ]

Authors

  • İlker Ünal

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 1,362 11/5/2017

Bug fix