setting up Azure DocumentDB Javascript SDK

azure-cosmosdb

Solution

The steps for getting the JS SDK code sample above to work are as follows:

- Place your DocumentDB endpoint in to `(hostendpoint)`.

- Place the collection resource id (this is the `_rid`, not the `id`, property in the collection JSON document) as the value for `(collectionId)`.

- Place the permissions token (you will need to create a user and permission for the collection) as the value for `(resourceToken)`.

- Place the `_self` link for the collection in `(collectionUrl)`

The completed code sample should resemble something like this:

var host = "https://bloopbloop.documents.azure.com:443"; // Add your host

var resourceTokens = {};
resourceTokens["Pa0wAKPRZQA="] = "type=resource&ver=1&sig=WaOXNCJaZ7Z7obf74i48Yg==;Dbb5bXDnm5ou0rpAUyifsFR5VNIsfSTeuad81P7zC7ytJtSwLCLnw9ne99vuIH8/giBsYIrqtXE5PYDs2idLfdJ4+K3bfT8BJgWqdgIuIEE/nvVpdEQ85y1azPXO7F+wXwBzK4eH2wQ0yMudy+petUdnN1GR3VJNsuNTZ1j+mnLLT/FLpFjWLVyI2dTLe7KHM0FvnczVZmT9wGJV8rUMjgjV9oG552DAev9exPGnj4E=;"; // Add the collectionId and resourceToken for read/write on the collection

var collectionUrl = "dbs/Pa0wAA==/colls/Pa0wAKPRZQA=/"; // Add the collection self-link

var client = DocumentDB.createClient(host, {
  resourceTokens: resourceTokens
});

var documentDefinition = {
  id: "Hello world document",
  content: "Hello World!"
};

client.createDocument(collectionUrl, documentDefinition, function(err, createdDocument) {
  if (err) {
    throw err;
  }

  console.log('result', createdDocument);
});

In case you are building a Node.js application - I'd highly recommend checking out the Node.js client: https://github.com/Azure/azure-documentdb-node/

The DocumentDB JS client is not the most intuitive SDK to use. The dev experience surrounding this SDK is something we're trying to improve. If you are open to discussing your use-case/scenario (or even would like some general help) - please reach out to me at `andrl {at} microsoft.com`!

Problem

I am trying to set up a Azure DocumentDB using Javascript SDK. So far I have a master key, a endpoint url, as well as a database set up and a collection. What I cannot figure is how to substitute what I have with the values they require and I cannot find anything to refrence in the documentation or otherwise. below is the code I am trying to fill the values in for. My thought was that collectionurl = my endpoint url, resourceToken = my key, collectionId = collection name. but then what is hostedendpoint and how should I set up the resourceToken? I know its not much to go on but if anyone has used DocumentDB I would really appreciate the help. ``` var host = (hostendpoint); // Add your host var resourceToken = {}; resourceTokens[(collectionId)] = (resourceToken); // Add the collectionId and resourceToken for read/write on the collection var collectionUrl = (collectionUrl); // Add the collection self-link var client = DocumentDB.createClient(host, {resourceTokens: resourceTokens}); var documentDefinition = {id: "Hello world document", content: "Hello World!"}; client.createDocument(collectionUrl, documentDefinition, function(err, createdDocument) { if (err) { throw err; } console.log('result', createdDocument.content); }) ``` http://dl.windowsazure.com/documentDB/jsclientdocs/

Original source