javascript - Cosmos DB 存储过程

标签 javascript sql stored-procedures azure-cosmosdb

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

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 调用此存储过程,但不断收到一条消息,要求我提供分区键。

最佳答案

Is there any way in which I can avoid having to supply a partition key?

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

关于javascript - Cosmos DB 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61369880/

相关文章:

c# - LINQ to Entities 而不是存储过程?

mysql - 控制字符等效触发器MySQL

javascript - 如何使用 Jquery 显示和隐藏复选框文本

javascript - 防止清除选择

javascript - 使用 javascript queryselectorall 而不是 jquery 选择器

sql - 比较 NULL

sql - PostgreSQL,从最大 ID 中选择

javascript - 如何将动态参数传递给 .pde 文件

sql - 在 Access-SQL 中将 NULL 值设置为自定义值

sql-server - 通过存储过程从数据库中检索值