【问题标题】:Documents inserting in CosmosDB emulator not working in CosmosDB on Azure online在 CosmosDB 模拟器中插入的文档在 Azure 在线 CosmosDB 中不起作用
【发布时间】:2019-07-08 11:48:09
【问题描述】:

我正在将应用程序从 Azure SQL DB 迁移到 Cosmos DB,但在 CosmosDB 中存储文档时遇到问题。

我的应用程序是使用 Microsoft.Azure.DocumentDB 库用 C# 编写的。 当我使用 Microsoft.Azure.DocumentDB 库中的 CreateDocumentAsync(Uri, object) 方法插入一条记录时,它会顺利通过。 当我使用 Azure CosmosDB 数据资源管理器查询数据库时,我看不到任何记录,尽管在 mongo shell 中 count() 状态记录在那里,并且 .find() 给出错误“处理此请求时发生未知服务器错误。 "

我使用库 Microsoft.Azure.DocumentDB 来制作各种杂物。我用模拟器对其进行了测试。但是,当我尝试连接在线 cosmosDB 时,我遇到了一个问题,即当我通过库中的 CreateDocumentAsync(Uri, object) 方法插入文档时,我不再能够在 comosDb dataexplorer 中看到插入的文档。

我尝试在没有 id 的情况下插入它,并尝试使用 objectId _id 插入它,但是我一直遇到同样的问题。

当我通过 mongo shell 查看集合时,我确实看到已添加一个文档,但当我使用 db.colleciton.find() 时,我收到错误:“处理此请求时发生未知服务器错误。” 更下游的代码能够检索文档。我错过了什么吗?我需要在 azure dB 中设置一个设置还是这是库的一个已知问题?

class SalesOrder
{
    public string Id { get; set; }

            public string PurchaseOrderNumber { get; set; }

            public DateTime OrderDate { get; set; }

            public string AccountNumber { get; set; }

            public decimal SubTotal { get; set; }

            public decimal TaxAmt { get; set; }

            public decimal Freight { get; set; }

            public decimal TotalDue {get; set;}
    }

class Program
{
    private static readonly string endpointUrl = "endpoint";

    private static readonly string authorizationKey = "authorizationKey";

    private static readonly string databaseId = "databaseId";

    private static readonly string collectionId = "collectionId";

    private static DocumentClient client;

    static void Main(string[] args)
    {
        using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey))
        {
            var collectionLink = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

            Insert(collectionLink).Wait();
        }
    }

    private static async Task Insert(Uri collectionLink)
    {
        var orders = new List<object>();

        orders.Add(new SalesOrder

        {
            Id = "POCO1",

            PurchaseOrderNumber = "PO18009186470",

            OrderDate = new DateTime(2005, 7, 1),

            AccountNumber = "10-4020-000510",

            SubTotal = 419.4589m,

            TaxAmt = 12.5838m,

            Freight = 472.3108m,

            TotalDue = 985.018m
        });



        orders.Add(new SalesOrder

        {

            Id = "POCO2",

            PurchaseOrderNumber = "PO15428132599",

            OrderDate = new DateTime(2005, 7, 1),

            AccountNumber = "10-4020-000646",

            SubTotal = 6107.0820m,

            TaxAmt = 586.1203m,

            Freight = 183.1626m,

            TotalDue = 4893.3929m,
        });

        foreach (var order in orders)

        {

            Document created = await client.CreateDocumentAsync(collectionLink, order);



            Console.WriteLine(created);

        }
    }
}

【问题讨论】:

  • 您能否向我们展示一下您是如何尝试在资源管理器中检索这些文档的?您提到了count()find(),我认为它们不是用户界面的一部分。你能告诉我们你在底部状态条得到的错误是什么吗?
  • 抱歉,对于 mongo shell 中使用的 .count() 和 .find() 方法的解释不佳。我会在问题中更新它。
  • 哦,你在使用 MongoDB API 吗?
  • 不,我正在使用 Microsoft.Azure.DocumentDB 库,它使用 DocumentClient 在 ComosDb 中创建文档
  • 但是您的帐户针对的是 Mongo API 而不是 SQL API,对吗?

标签: c# azure azure-cosmosdb document


【解决方案1】:

问题是您有一个使用 MongoDB API 的 Cosmos DB 帐户,但您正在尝试使用 SQL API SDK 创建数据。

如果您想在需要使用 MongoDB SDK 而不是 Cosmos DB 时使用 MongoDB API。

但是,如果您从 Azure SQL 迁移并且没有特定理由使用 MongoDB API,那么您应该只使用 CosmosDB SQL API 和您已经在使用的 SDK。如果你这样做,你的问题就会消失。

发生这种情况的原因是因为 SQL API SDK 正在创建 SQL API 查找文件,因此 CosmosDB MongoDB 接口无法在 UI 中处理它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多