简体   繁体   English

Azure 不支持 Cosmos DB 查询

[英]Azure Cosmos DB Query Within not supported

I have created an Azure Function (HTTP triggered) that is running a spatial query against a Cosmos DB using Linq. It returns rock elements that are within a geometric polygon boundary.我创建了一个 Azure Function(HTTP 触发),它使用 Linq 对 Cosmos DB 运行空间查询。它返回几何多边形边界内的岩石元素。

When running this, I get the following error:运行它时,出现以下错误:

Exception while executing function: example One or more errors occurred. Exception while executing function: example 发生一个或多个错误。 (Method 'Within' is not supported., Windows/10.0.14393 documentdb.netcore-sdk/2.11.6) Method 'Within' is not supported., Windows/10.0.14393 documentdb.netcore-sdk/2.11.6 (Method 'Within' is not supported., Windows/10.0.14393 documentdb.netcore-sdk/2.11.6) Method 'Within' is not supported., Windows/10.0.14393 documentdb.netcore-sdk/2.11.6

I have checked I am using all the latest NuGet library versions.我检查过我使用的是所有最新的 NuGet 库版本。 Can someone give me a hint on what else to check?有人可以提示我还有什么要检查的吗? This is the code:这是代码:

public static class Example
{
    [FunctionName("example")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "example")] HttpRequest req,
        [CosmosDB(
            databaseName: "a",
            collectionName: "b",
            ConnectionStringSetting = "CosmosDBConnection")]
            DocumentClient client,
            ILogger log)
        {
            log.LogInformation($"GetRockLocationsSpatial for coordinates");
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("a", "b");
            Polygon rectangularArea = new Polygon(
                new[]
                {
                    new LinearRing(new [] {
                        new Position(100, 50),
                        new Position(130, 50),
                        new Position(130, 40),
                        new Position(100, 40),
                        new Position(100, 50)
                    })
                });
            var rocks =client.CreateDocumentQuery<RockLocationDocument>(collectionUri).Where(a => a.location.Within(rectangularArea)).AsEnumerable();

            foreach (RockLocationDocument rock in rocks)
            {
                Console.WriteLine("\t" + rock);
            }

            return new OkObjectResult(JsonConvert.SerializeObject(rocks));
        }
    }
}

I'm not sure what causes your error.我不确定是什么原因导致您的错误。 But I can get the result with your code.但是我可以用你的代码得到结果。 You can follow this and have a try.你可以按照这个试试。

RockLocationDocument.cs RockLocationDocument.cs

using Microsoft.Azure.Documents.Spatial;

namespace example
{
    public class RockLocationDocument
    {

        public string id { get; set; }

        public Point location { get; set; }
    }
}

Founction.cs函数.cs

I pass FeedOption within CreateDocumentQuery method.我在CreateDocumentQuery方法中传递了 FeedOption。

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Spatial;
using System.Linq;

namespace example
{
    public static class Example
{
    [FunctionName("example")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "example")] HttpRequest req,
        [CosmosDB(
            databaseName: "Test",
            collectionName: "site",
            ConnectionStringSetting = "sogo_DOCUMENTDB")]
            DocumentClient client,
            ILogger log)
        {
            log.LogInformation($"GetRockLocationsSpatial for coordinates");
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("Test", "site");
            Polygon rectangularArea = new Polygon(
                new[]
                {
                    new LinearRing(new [] {
                        new Position(100, 50),
                        new Position(130, 50),
                        new Position(130, 40),
                        new Position(100, 40),
                        new Position(100, 50)
                    })
                });

            var rocks =client.CreateDocumentQuery<RockLocationDocument>(collectionUri,new FeedOptions { EnableCrossPartitionQuery = true }).Where(a => a.location.Within(rectangularArea)).AsEnumerable();

            foreach (RockLocationDocument rock in rocks)
            {
                Console.WriteLine("\t" + JsonConvert.SerializeObject(rock));
            }

            return new OkObjectResult(JsonConvert.SerializeObject(rocks));
        }
    }
}

Library:图书馆:

在此处输入图像描述

Test by using postman:使用postman测试:

在此处输入图像描述

Hope this can help you.希望这可以帮到你。

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

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