首页 > 解决方案 > 如何从 Azure CosmosDb 中的 BsonType 'Binary' 反序列化 'ObjectId'?

问题描述

在 1 周内,我在使用 AZURE CosmosDb 时遇到了很大的麻烦!这让我失望。一切都在我的本地计算机上工作。但是,如果我发布以下代码,它会返回错误以下。

Azure 中的 LOGS 函数:

2019-07-25T12:28:00.854 [信息] C# 定时器触发函数执行于:7/25/2019 12:28:00 PM 2019-07-25T12:28:00.888 [错误] 执行“Function1”(失败,ID =9e5758d7-7ffa-430a-85ee-178aeaa6758a) 无法从 BsonType 'Binary' 反序列化 'ObjectId'。

代码:

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; }


}

标签: c#mongodbazureazure-functionsazure-cosmosdb

解决方案


推荐阅读