在 Azure Cosmos DB (SQL API) 中,以下查询收费9356.66 RU:
SELECT * FROM Core c WHERE c.id = @id -- @id is a GUID
Run Code Online (Sandbox Code Playgroud)
相比之下,以下更复杂的查询仅收费6.84 RU:
SELECT TOP 10 * FROM Core c WHERE c.type = "Agent"
Run Code Online (Sandbox Code Playgroud)
这两个示例中的文档都非常小,但具有一些属性。此外,文档集合不使用任何自定义索引策略。该集合包含 105685 份文档。
对我来说,这听起来好像“id”字段上没有正常工作的索引。
这怎么可能以及如何解决?
更新:
当使用 Azure Cosmos DB 并查询一个分区时,我只需在 FeedOptions 中指定分区键。但是当我必须查询 n 个分区时,我有(据我所知)2 个选项:
当我必须对整个结果集(跨所有分区)应用排序标准和分页时,我认为第一种方法将达到其极限。
使用 .NET SQL API 在 Cosmos DB 中跨多个分区进行查询的推荐方法是什么?
如果我尝试使用已存在的 id 和分区键插入文档,如果容器上没有设置唯一约束,它会按预期工作。
但是,当我将表中的任何值设置为唯一值时,upsert 不起作用并且我得到一个(409 冲突 - 一个 id 与文档的 id 字段匹配的文档已经存在)。在这种情况下,唯一约束不应该是问题,但它会导致此错误,它对 upsert 有一个奇怪的错误描述,因为 id 是否已经存在应该无关紧要。
我正在使用documentClient.upsertDocument(collectionLink, documentDefinition, null, true);.
我正在尝试将SQL查询参数与WHERE ... INAzure Cosmos DB SQL API查询中的语句一起使用:
var componentDesignGuids = new List<Guid>();
// ...
var queryDefinition = new QueryDefinition(
"SELECT componentDesign.guid, componentDesign.component.name, componentDesign.component.componentType " +
"FROM components componentDesign " +
"WHERE componentDesign.guid IN (@componentDesignGuids)")
.WithParameter("@componentDesignGuids", string.Join(",", componentDesignGuids.Select(guid => $"\"{guid}\""))));
Run Code Online (Sandbox Code Playgroud)
但这会导致查询,其中被替换的参数是单个字符串,例如IN ("guid0, guid1, guid2")。由于这是一个IN子句,因此我想在其中放置不确定数量的字符串,例如IN ("\"guid0\", \"guid1\", \"guid2\"")。我意识到我可以使用插值字符串来完成这项工作,但是我希望输入尽可能安全以防止注入。如何使用QueryDefinition和/或指定此内容WithParameter?
我正在使用 Linq 创建对 cosmos 的查询
这将被转换为 SQL,然后运行该 SQL 来进行搜索
var modelName = "Mondeo";
var baseQuery = client.CreateDocumentQuery<Car>(StaticSettings.ProjectionsCollectionUri,
new FeedOptions { MaxItemCount = maxItemCount, PartitionKey = new PartitionKey(partitionKey) })
.Where(order => car.ModelName == modelName);
Run Code Online (Sandbox Code Playgroud)
如果我运行此代码并在此语句后放置一个断点,我可以看到生成的原始 SQL 查询
这显示在检查器的第一行
{{"query":"SQL HERE"}}
Run Code Online (Sandbox Code Playgroud)
我怎样才能通过代码达到这个目的?
我希望得到这个 SQL 以确保它是我想要的并且我可以在我的测试中使用它
保罗
这个问题与DocumentClientfrom Microsoft.Azure.DocumentDB.Core v2.11.2. (更新:该错误也存在于Microsoft.Azure.Cosmos.)
当查询包含DateTime带有尾随零的值时,Cosmos DB 的 LINQ 提供程序中似乎存在错误。考虑以下代码:
string dateTimeWithTrailingZero = "2000-01-01T00:00:00.1234560Z"; // trailing zero will be truncated by LINQ provider :-(
DateTime datetime = DateTime.Parse(dateTimeWithTrailingZero, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
IQueryable<Dictionary<string, object>> query =
client.CreateDocumentQuery<Dictionary<string, object>>(collectionUri)
.Where(x => (DateTime) x["datetime"] <= datetime);
Run Code Online (Sandbox Code Playgroud)
结果query 包括属性所在的文档,datetime例如"2000-01-01T00:00:00.1234567Z"(即使它不应该)。
结果query并没有包含文件,其中datetime是"2000-01-01T00:00:00.1234560Z"(即使它应该)。
有什么方法可以使用DocumentClientLINQ 来DateTime正确查询属性吗?(我知道使用原始 SQL 是有效的 - 由于各种原因,我必须 …
Below is the code that I have:
List<PatchOperation> patchOperations = new List<PatchOperation>();
patchOperations.Add(PatchOperation.Replace("/endpointId", 100));
string id = "id1";
PartitionKey partitionKey = new PartitionKey("partitionkey1");
await _container.PatchItemAsync<Watermark>(id,
partitionKey,
patchOperations);
Run Code Online (Sandbox Code Playgroud)
I am expecting to get endpointId property to be replaced with 100.
但是,我面临着 Message: {"Errors":["One of the specified inputs is invalid"]}。
我可以检查一下我缺少哪一部分吗?或者我是否必须等待为我的 Cosmos 数据库启用补丁私人预览功能?
我正在运行一个查询 CosmosDB 实例的 Azure 函数。
我正在尝试使用延续令牌实现分页,但在使用延续令牌调用我的函数时不断收到以下响应:
Message": "发生错误。", "ExceptionMessage": "无效的连续令牌\r\nActivityId: 0f79a65f-a9d2-49a8-8a9c-d33a8526bec8,Microsoft.Azure.Documents.Common/2.0.0.0,documentdb-dotnet- sdk/1.22.0 主机/32位 MicrosoftWindowsNT/6.2.9200.0
这是我的 Azure 函数:该函数最初将在没有令牌的情况下调用,并且根据第二页的请求,将传入令牌。
[FunctionName("GetAllPaged")]
public static async Task<HttpResponseMessage> ReadAll(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "GetAllPaged/{pageSize?}/{token?}")]HttpRequestMessage req,
int? pageSize, string token, ILogger log, [Inject]IComponent<EventModel> component)
{
try
{
log.LogInformation("Get all events");
var response = await component.GetAll_Paged(pageSize, token);
return req.CreateResponse(HttpStatusCode.OK, response);
}
catch (Exception ex)
{
log.LogError(ex.Message, ex);
return req.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
Run Code Online (Sandbox Code Playgroud)
在我首次调用 Azure 函数时,使用 URL http://localhost:7071/api/Event/GetAllPaged/3,我得到以下响应:
{
"Continuation": {
"token": "CDhbANnikwAGAAAAAAAAAA==",
"range": {
"min": "",
"max": …Run Code Online (Sandbox Code Playgroud) c# pagination azure-functions azure-cosmosdb azure-cosmosdb-sqlapi
我正在尝试手动指定文档的id字段,但是我找不到如何插入该属性的整数值。
我正在使用 azure.cosmos 的 python 模块,当我尝试插入整数值时,似乎在 sdk 实现中遇到错误,特别是在这里。
@staticmethod
def __ValidateResource(resource):
id = resource.get('id')
if id:
if id.find('/') != -1 or id.find('\\') != -1 or id.find('?') != -1
or id.find('#') != -1:
raise ValueError('Id contains illegal chars.')
if id[-1] == ' ':
raise ValueError('Id ends with a space.')
Run Code Online (Sandbox Code Playgroud)
我猜想这个字段并不是按照我的意图设计的,并且不需要输入整数,但是如果对这个问题有所了解的话那就太好了。
我们在为 Cosmos Document DB 编写查询时遇到问题,我们想要创建一个新的文档属性并在 ORDER BY 子句中使用它
例如,如果我们有一组文档,例如:
{
"Name": "Geoff",
"Company": "Acme"
},
{
"Name": "Bob",
"Company": "Bob Inc"
}
Run Code Online (Sandbox Code Playgroud)
...我们编写一个这样的查询,SELECT * FROM c ORDER BY c.Name效果很好并返回两个文档
但是,如果我们要添加具有附加属性的新文档:
{
"Name": "Geoff",
"Company": "Acme"
},
{
"Name": "Bob",
"Company": "Bob Inc"
},
{
"Name": "Sarah",
"Company": "My Company Ltd",
"Title": "President"
}
Run Code Online (Sandbox Code Playgroud)
...我们编写一个查询,SELECT * FROM c ORDER BY c.Title它只会返回 Sarah 的文档,并排除没有 Title 属性的 2。
这意味着 ORDER BY 子句的行为就像一个过滤器而不仅仅是一个排序,这似乎是出乎意料的。
似乎所有文档模式都可能随着时间的推移添加属性。除非我们返回并将这些属性添加到容器中的所有现有文档记录中,否则我们永远无法在不排除记录的情况下在 ORDER BY 子句中使用它们。
有谁有解决方案允许 ORDER BY …
azure-cosmosdb ×10
azure ×2
c# ×2
iqueryable ×1
java ×1
linq ×1
pagination ×1
paging ×1
partitioning ×1
python ×1