2017-04-11 92 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 –