2017-04-11 10 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やろう持ってください:SQLが期待される結果を得ることができます

1.Makeは確かに。フィールドは大文字と小文字が区別されます。私の製品モデルのようにProductIdフィールドを使用しましたので、私の場合はproduct.ProductIdではなくproduct.productIdを使用しました。私はAzureのポータル

enter image description here

2.ExecuteとAzureのポータルの両方が働いている場合は

enter image description here

からストアドプロシージャを、それを照会、もう一度コードを試しているしてください。

+0

'DocumentDB'は**大文字小文字を区別しています** - レッスンは難しいことを学んでいます...ありがとう@Tom Sun - MSFT –