We help IT Professionals succeed at work.

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

High Priority
137 Views
Last Modified: 2019-08-05
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

Comment
Watch Question

Author

Commented:
Are you There?
Global Support Services Engineer
CERTIFIED EXPERT
Commented:

Explore More ContentExplore courses, solutions, and other research materials related to this topic.