简体   繁体   English

计算 cosmos db 中的项目数?

[英]Count number of items in cosmos db?

I am currently intefacing my cosmos db, using EF core, and thought I could use linq queries to determine how many items currently resides within the db.我目前正在使用 EF 核心连接我的 cosmos 数据库,并认为我可以使用 linq 查询来确定数据库中当前驻留的项目数。

But when I do this但是当我这样做时

        var a = dbContext.Set<DbModel>().FirstOrDefault();

I get an error我收到一个错误

A host error has occurred during startup operation '4332670c-85c3-4128-ba47-817b45c4a9d9'.
[2022-07-19T09:35:36.988Z] System.Linq.Expressions: Argument types do not match.
Value cannot be null. (Parameter 'provider')

How do determine wether my database contains any items?如何确定我的数据库是否包含任何项目? either via EF core, or through the cosmos client?是通过 EF 核心还是通过 cosmos 客户端?


I try to reproduce same issue but not working.我尝试重现相同的问题但无法正常工作。 You can do by using this below code.您可以使用下面的代码来完成。 This code is working but not using entity framework.此代码正在运行,但未使用实体框架。

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp7
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string EndPointUri = "https://mydomain.documents.azure.com:443/";
            string PK = "CHATYXMWx3YFAwy55Ee4uI92TrUTwtbi7loAutVdPSB0Z3GdQcgZCGthWqZXv9v343urjrRfqV9x2I3xZZYFEQ==";

            DocumentClient cosmosClient = new DocumentClient(new Uri(EndPointUri), PK);

            string sqlQuery = "SELECT * FROM d";
            var dbs = cosmosClient.CreateDatabaseQuery(sqlQuery).ToList();

            foreach (var db in dbs)
            {
                Console.WriteLine("Database: " + db.id);
                Console.WriteLine("- Collections: ");

                List<DocumentCollection> collections = cosmosClient.CreateDocumentCollectionQuery((String)db._self).ToList();

                foreach (var collection in collections)
                {

                    var count = cosmosClient.CreateDocumentQuery<int>(collection.SelfLink, $"SELECT value count(1) FROM c",
                                    new FeedOptions() { EnableCrossPartitionQuery = true, MaxItemCount = 1, }).AsEnumerable().First();

                    Console.WriteLine("  - " + collection.Id + $", Count: " + count);
                }
                Console.WriteLine(Environment.NewLine);
            }
            Console.WriteLine("Finished reading all DB Collections");

            Console.ReadKey();
        }
    }
}

Cosmos Db宇宙数据库在此处输入图像描述

Output输出在此处输入图像描述

These document will also help you by Microsoft Github onGet the Record Count in Cosmos DB这些文档还将通过 Microsoft Github 帮助您获取 Cosmos DB 中的记录计数

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM