首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cosmos DB存储过程

Cosmos DB存储过程
EN

Stack Overflow用户
提问于 2020-04-22 16:13:29
回答 1查看 270关注 0票数 0

我试图运行一个过程,它查询文档并根据一些规则调整一些属性,这些规则由传递给查询的参数决定。

代码语言:javascript
复制
function downsample(ageInDays, downsampleFactor) {
    var collection = getContext().getCollection();
    var responseBody = {
        deleted: 0,
        message: ""
    };
    var downsampledDocuments = [];
    var count = 0;

    collection.queryDocuments(
        collection.getSelfLink(),
        
        'SELECT * FROM root r ' + 
        'WHERE (DATEDIFF(day, r.EventProcessedUtcTime, GETDATE()) > ' + ageInDays+ 'AND r.downsamplingFactor < ' + downsampleFactor + ')' +
        'OR' +
        '((DATEDIFF(day, r.EventProcessedUtcTime, GETDATE()) > ' + ageInDays + ' AND r.downsamplingFactor = null )' +
        'ORDER BY r.did, r.resourceType ASC',
        
        function (err, documents, options) {
            if (err) throw err;
            // Check the feed and if empty, set the body to 'no docs found', 
            // else perform the downsampling

            if (!documents || !documents.length) {
                var response = getContext().getResponse();
                responseBody.message = "No documents found";
                response.setBody(responseBody);
            } else {
                // We need to take into consideration that in a lot of cases, the data will already be downsampled so we
                // example: previous downsampling factor of documents: 4, if a new downsampling is performed on these docs with factor 8 then we need to
                var adjustedDownSamplingFactor;
                if (documents[0].downsamplingFactor == null) {
                    adjustedDownSamplingFactor = downsampleFactor;
                } else {
                    adjustedDownSamplingFactor = downsampleFactor / documents[0].downsamplingFactor;
                }
                var aggregatedDocument = documents[0];
                var documentValueSum = 0;
                var documentCount = 0;
                var aggregatedDocumentValue = 0;

                for(doc in documents){

                    if(!aggregatedDocument){
                        aggregatedDocument = doc; 
                    }

                    if(documentCount >= adjustedDownSamplingFactor || aggregatedDocument.did !== doc.did || aggregatedDocument.resourceType !== doc.resourceType){
                        // preparing aggregated document
                        aggregatedDocumentValue = documentValueSum / documentCount;
                        aggregatedDocument.value = aggregatedDocumentValue;
                        aggregatedDocument.downsamplingFactor = downsampleFactor;
                        
                        //Adding the downsampled data to the Array which will be uploaded to the cosmosdb
                        downsampledDocuments.push(aggregatedDocument);

                        aggregatedDocument = null;
                        documentCount = 0;
                        documentValueSum = 0;
                        
                        continue;
                    }

                    documentValueSum += doc.value;
                    documentCount++;

                    
                }
                var response = getContext().getResponse();

                tryDelete(documents);
                // Call the CRUD API to create a document.
                tryCreate(downsampledDocuments[count], callback);


                responseBody.message = "Downsampling was succesful"
                response.setBody(responseBody);
            }

    });

因此,我不会将任何文档传递给查询,因此我不知道必须向存储过程提供哪个分区密钥。有什么方法可以避免我不得不提供分区密钥吗?我从一个API调用这个存储过程,但是一直收到一个消息,我应该提供一个分区密钥。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-22 16:17:20

有什么方法可以避免我不得不提供分区密钥吗?

不幸的是没有。您必须提供一个分区键值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61369880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档