gpt4 book ai didi

arrays - 如何 for 循环集合中的所有文档 - Azure CosmosDB - Nodejs

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:41 26 4
gpt4 key购买 nike

我已经查看了有关此问题的一些答案/问题,但尚未找到解决方案。

我有一个包含文档(简化)的集合,如下所示:

{
"id": 123
"stuff": "abc"
"array":[
{
"id2":456
"properties": [
{
"id3": 789
"important": true
}
]
}
]
}

我想检查我的集合中的每个文档,array中的每个array对象, code>for 每个 properties,例如,如果它具有 important: true。然后返回:

"id": 123
"id2": 456
"id3": 789

我尝试过使用:

client.queryDocuments(self.collection._self, querySpec).toArray(function(err, results) {
if (err) {
callback(err);
} else {
callback(null, results[0]);
}
});

但问题是数组有最大字符数限制。如果我的收藏有数百万份文档,这可能会超出。 (Javascript Increase max array size)

或者,我误解了上面的问题吗?它是在谈论数组中的对象数量(其中每个对象可以有无限的对象字符长度吗?)

因此,我正在寻找一个 for 循环 式的解决方案,其中返回每个文档,我进行分析,然后移至下一步/并行执行它们。

任何见解将不胜感激。

最佳答案

But the issue is an array has a maximum character limit. If my collection has millions of documents, this would presumably be exceeded. (Javascript Increase max array size)

基于我的 research ,js 中最长的数组可能有 232-1 = 4,294,967,295 = 4.29 十亿个元素。不过,它完全足以满足您数百万数据量的需求。另外,这么海量的数据你肯定不能直接查询,那是不可能的。

无论是吞吐量限制(RU 设置)还是查询效率因素,您都应该考虑批量处理大量数据。

Thus I am looking a for loop-esque solution, where each document is returned, I do my analysis, then move to then next/do them in parallel.

也许你可以使用v2 js sdk cosmos db sql api。请引用示例代码:

const cosmos = require('@azure/cosmos');
const CosmosClient = cosmos.CosmosClient;

const endpoint = "https://***.documents.azure.com:443/"; // Add your endpoint
const masterKey = "***"; // Add the masterkey of the endpoint
const client = new CosmosClient({ endpoint, auth: { masterKey } });
const databaseId = "db";
const containerId = "coll";

async function run() {
const { container, database } = await init();
const querySpec = {
query: "SELECT r.id,r._ts FROM root r"
};
const queryOptions = {
maxItemCount : -1
}
const queryIterator = await container.items.query(querySpec,queryOptions);
while (queryIterator.hasMoreResults()) {
const { result: results, headers } = await queryIterator.executeNext();
console.log(results)
console.log(headers)
//do what you want to do

if (results === undefined) {
// no more results
break;
}
}
}

async function init() {
const { database } = await client.databases.createIfNotExists({ id: databaseId });
const { container } = await database.containers.createIfNotExists({ id: containerId });
return { database, container };
}

run().catch(err => {
console.error(err);
});

更多关于延续 token 的详细信息,请引用我的previous case .如有任何疑问,请告诉我。

关于arrays - 如何 for 循环集合中的所有文档 - Azure CosmosDB - Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52190527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com