[CSHARP-1399] A document with same ID has two different value! Created: 11/Sep/15  Updated: 11/Sep/15  Resolved: 11/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: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows



 Description   

Here is psedu code :

   Cat Gor = new Cat(){ IsAlive = true};
   MongoGorCollection.Save(Gor);
   //Saving Gor as a main level document
 
   Cage CatCage = new Cage(){ Animals.AddRange( new Cat[] { Gor }  )};
   MongoCageCollection.Save(CatCage);
   //Adding Gor to Animals list of Cage as a sub-document
 
   var RetrieveGor = MongoGorCollection.FirstOrDefault();   
   RetrieveGor.IsAlive = false;
   MongoGorCollection.Save(RetrieveGor);
   //Well, we have changed IsAlive of Gor to false and saved back in DB.
   // So if I retrieve the only cat in the Cage then it must be dead.
 
   
   bool IsAliveShouldBeFalse = MongoCageCollection.FirstOrDefault().Animals.FirstOrDefault().IsAlive;
 
   //IsAliveShouldBeFalse is not false! It's true.

I have checked DB with MongoVUE and I see two document with same ID has different values!

Here is the link to screenshot :
http://i57.tinypic.com/2qtekn7.png



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

Hi Mohsen,

There is no referential integrity in MongoDB. What you have done is this:

1) Saved a Gor(1) to the GorCollection
2) Added Gor(1) into the Animals array in the Cage collection. There is no reference from this back to the Gor collection, so let's call this one Gor(2).
3) Fetched Gor(1) from GorCollection
4) Updated Gor(1) in the GorCollection
5) Retrieved Gor(2) from the Cage collection.

Notice that we are talking about 2 different Gors at this point. The Gor in the Gor collection is not the same as the Gor in the animals array in the Cage Collection.

If you are going to denormalize like this (and denormalization isn't bad), you have to take responsibility for updating all the references yourself.

Hope that explains things.
Craig

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