public void ReplaceTypeAssertionWithActual(BsonDocument actual, BsonDocument expected)
 {
     for (int i = 0; i < expected.ElementCount; i++)
     {
         var expectedElement = expected.ElementAt(i);
         var value           = expectedElement.Value;
         if (value.IsBsonDocument)
         {
             var valueDocument = value.AsBsonDocument;
             var actualValue   = actual.GetValue(expectedElement.Name, null);
             if (valueDocument.ElementCount == 1 && valueDocument.Select(c => c.Name).Single().Equals("$$type"))
             {
                 var type = valueDocument["$$type"].AsString;
                 if (type.Equals("binData"))
                 {
                     expected[expectedElement.Name] = actualValue;
                 }
                 else if (type.Equals("long"))
                 {
                     expected[expectedElement.Name] = actualValue;
                 }
             }
             else if (actualValue != null && actualValue.IsBsonDocument)
             {
                 ReplaceTypeAssertionWithActual(actualValue.AsBsonDocument, valueDocument);
             }
         }
         else if (value.IsBsonArray)
         {
             ReplaceTypeAssertionWithActual(actual[expectedElement.Name].AsBsonArray, value.AsBsonArray);
         }
     }
 }
        public Byte[] FindOneAndDeleteByteArry()
        {
            IMongoCollection <BsonDocument> mongoCollection = GetCollection <BsonDocument>();
            BsonDocument findResult = mongoCollection.FindOneAndDelete(Builders <BsonDocument> .Filter.Empty);

            Byte[] byteResult = null;
            if (findResult != null && findResult.Elements.Count() > 1)
            {
                byteResult = findResult.ElementAt(1).Value.AsByteArray;
            }
            return(byteResult);
        }
        private static BsonDocument CorrectDateField(BsonDocument bsonDocument)
        {
            BsonDocument result = new BsonDocument();

            int cpt = bsonDocument.Count();
            for (int i = 0; i < cpt; i++)
            {
              
                var element = bsonDocument.ElementAt(i);
                var value = element.Value;
                var name = element.Name;

                if(value is BsonDocument)
                {
                    value =CorrectDateField(value as BsonDocument);
                    element = new BsonElement(name, value);
                }
                else
                {
                    DateTime Out;
                    if (DateTime.TryParse(value.ToString(), out Out))
                    {
                        value = new BsonDateTime(Out);
                        element = new BsonElement(name, value);
                    }
                }                


                result.Add(element);

            }
            return result; 
        }
        private static void CopyMidelElmentInResult(BsonDocument document, BsonDocument result)
        {
            int i = 0;
            int count = document.Elements.Count()-1;
            while (i < count)
            {
                result.Add(document.ElementAt(i));
                i++;

            }
        }