0

Can anyone tell me which node module I have to require to parameterize queries for DocumentDB in Node.js?

I have structured the query very similar to this Microsoft example. I am using the documentdb module, but SqlQuerySpec and SqlParametersCollection are not recognised.

IQueryable<Book> queryable = client.CreateDocumentQuery<Book>(
                collectionSelfLink,
                new SqlQuerySpec
        {
                    QueryText = "SELECT * FROM books b WHERE (b.Author.Name = @name)", 
                    Parameters = new SqlParameterCollection() 
            { 
                          new SqlParameter("@name", "Herman Melville")
                    }
        });
Aaron Chen
  • 9,835
  • 1
  • 16
  • 28
DarkW1nter
  • 2,933
  • 11
  • 67
  • 120

1 Answers1

2

Try this in Node.js with the SDK azure-documentdb-node:

var querySpec = {
    'query': 'SELECT * FROM books b WHERE (b.Author.Name = @name)',

    "parameters": [
        { "name": "@name", "value": 'Herman Melville' }
    ]
}

client.queryDocuments(collectionUrl, querySpec).toArray(function(err, results) {
    if(err) return console.log(err);

    console.log(results);
});
Aaron Chen
  • 9,835
  • 1
  • 16
  • 28
  • I get "Bad request" when trying to do it this way. – Sam Nov 27 '18 at 19:33
  • This was my bad, but it was result of the choice used for the example above. Regardless of what your property name is, you want to add the parameter like this "name: '@color', value: 'red'", for example. The key should always be "name". – Sam Nov 27 '18 at 19:38