Link to home
Start Free TrialLog in
Avatar of programmerist 1983
programmerist 1983Flag for Türkiye

asked on

How can deserialize a 'ObjectId' from BsonType 'Binary' in Azure CosmosDb?

Hi;

I have really  big trouble with AZURE CosmosDb during 1 week! İt makes me down. Everything is working in my local computer. But if I publish below code,
it returns me Below Error.


LOGS in Azure function in Azure:

2019-07-25T12:28:00.854 [Information] C# Timer trigger function executed at: 7/25/2019 12:28:00 PM
2019-07-25T12:28:00.888 [Error] Executed 'Function1' (Failed, Id=9e5758d7-7ffa-430a-85ee-178aeaa6758a)
Cannot deserialize a 'ObjectId' from BsonType 'Binary'.
Code:
    public static class Function1
    {
        [FunctionName("Function1")]
        [Obsolete]
        public static void Run([TimerTrigger("* * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            var connectionString = @"mongodb://gggdfdfgfsdfdsfsd";
            //var connectionString = @"mongodb://localhost:27017/admin";
            MongoClient client = new MongoClient(connectionString);
            var database = client.GetDatabase("xxxxx-playground");
            //var database = client.GetDatabase("abc");
            var collection = database.GetCollection<Test>("test");

            var beforeDate = DateTime.UtcNow.AddDays(-7);
            var totalDeleted = 0;
            var ids = new List<ObjectId>();

            do
            {
                ids = collection.Find(k =>
                                      k.CreatedDate < beforeDate &&
                                      k.xxx == null &&
                                      k.yyy == null &&
                                      (k.Items.Length == 0 || k.Items == null)
                                      )

                                .Limit(100)
                                .Project(k => k._id)
                                .ToList();

                if (ids.Any())
                {
                    collection.DeleteMany(k => ids.Contains(k._id));
                    totalDeleted += ids.Count();
                }

            } while (ids.Any());

            log.LogInformation($"Deleted {totalDeleted} documents created before {beforeDate.ToShortDateString()}");
        }
    }

   
    public class Test
    {

        public ObjectId _id { get; set; }


        public object xxx { get; set; }
    
        public object yyy { get; set; }
      
        public DateTime CreatedDate { get; set; }
      
        public object[] Items { get; set; }


    }

Open in new window

Avatar of programmerist 1983
programmerist 1983
Flag of Türkiye image

ASKER

Are you There?
ASKER CERTIFIED SOLUTION
Avatar of Douglas Suyemoto
Douglas Suyemoto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial