[CSHARP-1408] MongoDB C# Driver Fails To Retriev SubDocument Created: 12/Sep/15  Updated: 14/Sep/15  Resolved: 14/Sep/15

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.10
Fix Version/s: None

Type: Bug Priority: Blocker - P1
Reporter: Mohsen [X] Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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!



 Comments   
Comment by Craig Wilson [ 14/Sep/15 ]

Hi Mohsen,

DbRefs are there for backwards compatibility, but they really should be considered deprecated and not used. We will not be adding support for them in LINQ.

Rather than use a DbRef, you can simply know what database and collection the "referenced" field is from and, if you need to store the information, simply store the information along-side the field. DbRefs have no special meaning to the server.

Craig

Comment by Mohsen [X] [ 13/Sep/15 ]

Checking with MongoVUE show that data is not even stored in database.
There must be a serious problem with inheriting from MongoDbRef;
public class DbRef<T> : MongoDB.Driver.MongoDBRef

Generated at Wed Feb 07 21:39:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.