【问题标题】:Azure Cosmos DB Mongo API Stored ProcedureAzure Cosmos DB Mongo API 存储过程
【发布时间】:2019-06-05 19:55:46
【问题描述】:

我需要为 Azure Cosmos DB Mongo API DB 编写 where 条件 我已经编写了 sp 来获取所有数据,但是当我添加 where 条件时它没有返回数据。

我需要帮助在 Mongo API 中编写 SP 并返回与我保存的相同 json 或具有选定属性的新 json

function sample(prefix) {
    var collection = getContext().getCollection();
var filterQuery2 = 'SELECT * FROM root r where r.userName= "' + prefix + '"';
    // Query documents and take 1st item.
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        filterQuery2,
    function (err, feed, options) {
        if (err) throw err;

        // Check the feed and if empty, set the body to 'no docs found', 
        // else take 1st element from feed
        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
        }
        else {
            var response = getContext().getResponse();
            var body = { prefix: prefix, feed: feed[0] };
            response.setBody(JSON.stringify(body));
        }
    });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

【问题讨论】:

  • 不太确定我是否理解这个问题,但是... Cosmos DB 的存储过程可以从 Core (SQL) API 调用,而不是 MongoDB API。
  • 如果我这样做,它会得到匹配的记录,还是会从数据库中获取所有记录,然后按用户名过滤? var collection = GetTasksCollection(); return collection.Find(x=>x.abc==userName).SortByDescending(i=>i.CreatedDate).FirstOrDefault();

标签: azure-cosmosdb azure-cosmosdb-mongoapi


【解决方案1】:

我测试了cosmos db mongo api存储过程,和你一样。

你的正常格式的 json 文档被转换成$v and $t,这就是你的过滤器出现故障的原因。

在这个thread和这个case你可以看到同样的情况,cosmos db团队发表了官方声明:

不正确的格式是混用 Cosmos DB 的结果:MongoDB 带有 Cosmos DB 的 API:DocumentDB API。

我建议您尝试与 azure 团队联系或提交反馈here

【讨论】:

  • 对我来说,条件也没有返回数据......目前唯一的方法是从代码中读取数据,这将是一个很大的性能问题,所以......手指交叉
  • 如果我这样做,它会得到匹配的,还是会从数据库中获取所有记录,然后按用户名过滤? var collection = GetTasksCollection(); return collection.Find(x=>x.abc==userName).SortByDescending(i=>i.CreatedDate).FirstOrDefault();
  • 1) 正如我在上面的 cmets 中所述(以及 @Jay 在这里指出的) - 这不是对 Cosmos DB 存储过程的有效使用。 2)为什么你会受到很大的影响? 3) 请不要在 cmets 中提出其他问题(请记住 - 您不应该将 Cosmos DB 存储过程应用于 MongoDB API 数据)。
猜你喜欢
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多