【发布时间】: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