2017-04-11 57 views
1

DocumentDB SPDocumentDB SP中陈述

function getSetOfProductDetails(productIds) { 
    var collection = getContext().getCollection(); 

    var isAccepted = collection.queryDocuments(
     collection.getSelfLink(), 
     "SELECT * FROM product WHERE product.productId IN ("+ productIds +")" , 
     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) getContext().getResponse().setBody(feed); 
     }); 

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

我的C#代码:

string productIdList = "'271762','288738','235521','266714'"; 
List<Product> productList = new List<Product>(); 
try 
{ 
    productList = await DocumentDBRepository<List<Product>>.ExecuteStoredProc("product","getSetOfProductDetails", productIdList); 
} 

DocumentDBRepository代码:

public static async Task<T> ExecuteStoredProc(string collectionId, string storedProcedureId, params object[] parameter) 
     { 
      ConnectToDatabase(); 
      StoredProcedureResponse<T> document = await _documentClient.ExecuteStoredProcedureAsync<T>(UriFactory.CreateStoredProcedureUri(_databaseId, collectionId, storedProcedureId), parameter); 
      return (T)(dynamic)document; 
     } 

结果集我收到productList是空白的,而有Collection中的文档。这是实施DocumentDBIN声明的正确方法吗?

+0

我猜测问题在于它如何序列化productIds数组。您可以尝试JSON.stingify(productIds),但我会首先尝试在queryDocuments调用之外构建SQL语句,并查看SQL语句生成时如何设置序列化。 –

+0

你可以直接返回已建立的SQL语句 –

回答

0

如果SELECT * FROM product WHERE product.productId IN ("+ productIds +")可以得到应该与您的代码一起使用的正确值。我测试你的代码,它在我身边正常工作。

enter image description here

至于你提到的文件都在收集,请有尝试做troubleshot以下列方式:

1.确保SQL可以得到预期的结果。这些字段区分大小写。在我的产品模型中,我使用了字段ProductId,所以在我的情况下,我使用product.ProductId而不是product.productId来查询。我查询它与Azure的门户

enter image description here

2.Execute从Azure的门户

enter image description here

如果两者都工作过的存储过程,请再次与你的代码一试。

+0

'DocumentDB'是**区分大小写** - 经验教训难道...非常感谢@Tom Sun - MSFT –