Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1408

MongoDB C# Driver Fails To Retriev SubDocument

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: Blocker - P1 Blocker - P1
    • None
    • 1.10
    • None
    • None

    Description

      Here is the code

      C#

       
          public class MainMongo 
          {
              public MongoDB.Bson.ObjectId Id { get; set; }
          }
       
          public class DbRef<T> : MongoDB.Driver.MongoDBRef
          {
              public DbRef(string ColName, MongoDB.Bson.BsonValue Id) : base(ColName, Id)
              {
       
              }
       
              [MongoDB.Bson.Serialization.Attributes.BsonIgnore()]
              public T Value
              {
                  get
                  { 
                      return new MongoDB.Driver.MongoClient("mongodb://localhost").GetServer().GetDatabase("Wik").FetchDBRefAs<T>(this);
                  }
              }
       
          }
       
       
          public class Car : MainMongo
          { 
              public DateTime BuildDate { get; set; }
       
          }
       
          public class Player : MainMongo
          { 
       
              public string Name { get; set; }
              public List<DbRef<Car>> Cars { get; set; }
       
          }
       
       
          //Here is code that uses database
       
                  var CarCol = mdb.GetCollection<Car>("Car");
                  var PlayerCol = mdb.GetCollection<Player>("Player");
                  CarCol.Drop();
                  PlayerCol.Drop();
       
       
                  var Ca = new Car() { BuildDate = DateTime.Now };
                  CarCol.Save<Car>(Ca);
       
                  var Pl = new Player() { Name = "Mohsen", Cars = new List<DbRef<Car>>(new DbRef<Car>[] { new DbRef<Car>("Car", Ca.Id) }) };
                  PlayerCol.Save<Player>(Pl);
       
                  var GetCarDbRef = PlayerCol.AsQueryable().OfType<Player>().FirstOrDefault().Cars.FirstOrDefault();
                  bool IsNull1 = GetCarDbRef.CollectionName == null; //true
                  bool IsNull2 = GetCarDbRef.DatabaseName == null;   //true
                  bool IsNull3 = GetCarDbRef.Id == null;             //true
       
       
      
      

      All values of DbRef which has inherited from MongoDbRef are null!

      Attachments

        Activity

          People

            Unassigned Unassigned
            Mohsen7s Mohsen [X]
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: