如何连接到 Cosmos MongoDb

How to connect to Cosmos MongoDb

我创建了一个本地 Cosmos 实例,并基于 this article

为我的 c# 应用程序设置了一个 MongoDb

然而,当尝试获取数据时,它似乎永远挂起

不清楚我的连接字符串应该包含什么,但我用这个填充了它:

"ClientDataRepositoryConfiguration": {
    "CollectionName": "ClientData",
    "ConnectionString": "mongodb://localhost:C2y6yDjf5%2FR%2Bob0N8A7Cgv30VRDJIWEHLM%2B4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw%2FJw%3D%3D@localhost:10255/admin?ssl=true",
    "DatabaseName": "ClientData"
 },

有人能告诉我是否输入正确吗?

这是我的宇宙的样子:

我有一个名为 ClientData 的数据库,其中有一个名为 ClientData 的容器。


控制器端点

[HttpGet("Test")]
public async Task<IActionResult> TestGet()
{
    // I can hit breakpoint here, it hangs when waiting for `GetallAsync`
    var result = await _clientDataRepository.GetAllAsync();
    return Json(result); 
}

存储库

internal sealed class ClientDataRepository : IClientDataRepository
{
    private readonly IMongoCollection<ClientData> _clientDatas;

    public ClientDataRepository(
        IClientDataRepositoryConfiguration configuration)
    {
        var client = new MongoClient(configuration.ConnectionString);
        var database = client.GetDatabase(configuration.DatabaseName);

        _clientDatas = database.GetCollection<ClientData>(configuration.CollectionName);
    }

    public async System.Threading.Tasks.Task<List<ClientData>> GetAllAsync(CancellationToken cancellationToken) 
        => await _clientDatas
            .Find(i => true)
            .ToListAsync(cancellationToken);
}

Cosmos 模拟器不支持数据浏览器,除了 SQL API 提到的 here。您可能已经使用 SQL API.

创建了集合

You can develop applications using Azure Cosmos emulator with the SQL, Cassandra, MongoDB, Gremlin, and Table API accounts. Currently the data explorer in the emulator fully supports viewing SQL data only; the data created using MongoDB, Gremlin/Graph and Cassandra client applications it is not viewable at this time.

您可以使用代码(不是数据资源管理器)创建集合和数据库,也可以使用 local mongodb installation 中的 mongod 命令。

如果您改用 mongod 工具,连接字符串应该类似于 documentatio 中提到的 mongodb://localhost:<port>mongod 工具的默认端口 27017。

"ClientDataRepositoryConfiguration": {
    "CollectionName": "ClientData",
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "ClientData"
 },

另一种选择是使用 Azure Resource instead of local