简体   繁体   English

Azure Cosmos DB GetById查询不起作用

[英]Azure Cosmos DB GetById query not working

I have an application that is using Mongo DB currently. 我有一个正在使用Mongo DB的应用程序。 I am looking to move the app to Azure and trying to use Cosmos DB. 我希望将应用程序移动到Azure并尝试使用Cosmos DB。 I upgraded the C# Mongo DB Driver in the code to the latest version 2.7.0 and it is working all fine still using Mongo DB. 我将代码中的C#Mongo DB驱动程序升级到了最新版本2.7.0,并且仍然可以使用Mongo DB正常工作。

I then used the Cosmos DB Migration Tool to migrate Data to the Azure Cosmos DB Emulator and changed the connection string in my web config to point to the emulator. 然后,我使用Cosmos DB迁移工具将数据迁移到Azure Cosmos DB仿真器,并在Web配置中更改了连接字符串以指向仿真器。 The Application is loading and some reference data is getting returned on my first screen but my GetById query below is not working? 正在加载应用程序,并且在我的第一个屏幕上返回了一些参考数据,但是下面的GetById查询不起作用?

    public virtual T GetById(TKey id)
    {
        if (typeof(T).IsSubclassOf(typeof(EntityBase)))
        {
            return GetById(new ObjectId(id as string));
        }

        //code removed for brevity
    }

    public virtual T GetById(ObjectId id)
    {
        var filter = Builders<T>.Filter.Eq("_id", id);

        var result = collection.FindSync<T>(filter).FirstOrDefault();

        return result;
    }

The result when I connect to my Mongo DB in web config is the single entity by the Object Id - however when I change the connection string to the emulator nothing is returned? 当我在Web配置中连接到Mongo DB时,结果是对象ID的单个实体-但是,当我将连接字符串更改为仿真器时,没有返回任何内容吗?

This is how the object looks in MongoDB (visualized using RoboMongo) 这就是对象在MongoDB中的外观(使用RoboMongo可视化)

{
    "_id" : ObjectId("5b97a56b6381fecd00f0e10a"),
    "LastUpdatedOn" : [ 
        NumberLong(636722473812102569), 
        -240
    ],
    "CreatedOn" : [ 
        NumberLong(636722473396922518), 
        -240
    ],
    "LastUpdatedBy" : "SYSTEM",
    "CreatedBy" : "TestUser",
    "VersionNumber" : 3,
    "Name" : "Audi",

This is how the same object looks in the Azure Cosmos DB Emulator after the migration using the migration Data Tool 这是使用迁移数据工具进行迁移后,同一对象在Azure Cosmos DB仿真器中的外观

{
    "_id": "5b97a56b6381fecd00f0e10a",
    "LastUpdatedOn": [
        636722473812102500,
        -240
    ],
    "CreatedOn": [
        636722473396922500,
        -240
    ],
    "LastUpdatedBy": "SYSTEM",
    "CreatedBy": "TestUser",
    "VersionNumber": 3,
    "Name": "Audi",

Could the reason it is not working be that that Id had lost the Object("")? 难道是因为Id丢失了Object(“”)吗? I tried to update the Azure Cosmos DB collection to add that but it was giving an error saying value expected as if I wasn't specifying correct JSON format. 我尝试更新Azure Cosmos DB集合以添加该集合,但是它给出了一个错误,提示期望值,好像我没有指定正确的JSON格式。

Judging from the CosmosDB emulator representation of the document it looks like you need to change your GetById method to use a string instead of an ObjectId . 从文档的CosmosDB仿真器表示来看,您似乎需要更改GetById方法以使用string而不是ObjectId

Something like this should work: 这样的事情应该起作用:

public virtual T GetById(TKey id)
{
    if (typeof(T).IsSubclassOf(typeof(EntityBase)))
    {
        return GetById(id as string);
    }

    //code removed for brevity
}

public virtual T GetById(string id)
{
    var filter = Builders<T>.Filter.Eq("_id", id);

    var result = collection.FindSync<T>(filter).FirstOrDefault();

    return result;
}

The actual reason this was not working was using the Azure Cosmos DB Migration Too is designed for use with the Cosmos DB SQL API. 无法使用的实际原因是使用Azure Cosmos DB Migration Too旨在与Cosmos DB SQL API一起使用。 I wanted to target the MongoDB API for Cosmos DB API. 我想将MongoDB API定位为Cosmos DB API。

The way to get the data into the Emulator for this was to use the mongoimport and mongoexport exe's as detailed here: 为此,将数据获取到仿真器中的方法是使用mongoimport和mongoexport exe,如下所示:

https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-migrate https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用日期时间字段查询Azure Cosmos DB中的对象 - How to query objects in Azure Cosmos DB with datetime field Azure Cosmos DB 触发器在升级到 3.6 后不工作 - Azure Cosmos DB Trigger not working after upgrading to 3.6 Azure Cosmos DB SDK 2、查询一个不存在且不存在未知异常的分区键? - Azure Cosmos DB SDK 2, Query a parition key that does not exists without unknow exception? 查询 Azure Cosmos Db 中的多个项目 - Querying multiple items in Azure Cosmos Db 并行连接到 Microsoft Azure Cosmos DB - Parallel connection to Microsoft Azure Cosmos DB Azure Cosmos DB - 删除整个分区 - Azure Cosmos DB - Delete entire partition 获取天蓝色的死信信息并保存到cosmos db中 - Get azure deadletter information and save into cosmos db Azure Cosmos DB - 忽略 Cosmos DB 的 FeedIterator.ReadNextAsync() 中的序列化失败 - Azure Cosmos DB - Ignoring serialization failures in Cosmos DB's FeedIterator.ReadNextAsync() Azure Cosmos DB - 如何在不同的控制器中使用不同的容器 Id 实例化 Cosmos Db - Azure Cosmos DB - How to instantiate Cosmos Db with a different container Id in different controllers 抛出异常:'Microsoft.Azure.Cosmos.CosmosException',将 JSON 批量导入到 Z3A580F142203677F1F0BC30898F63F5Z Cosmos Cosmos 时出现错误请求 - Exception thrown: 'Microsoft.Azure.Cosmos.CosmosException', Bad Request while bulk importing JSON to Azure Cosmos DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM