Simple DocumentDb stored procedure

喜你入骨 提交于 2019-12-10 17:24:03

问题


I'm trying to create a simple DocumentDb stored procedure to better understand its concepts. In this example, I'm trying to return a all "Female" users. Do I need to concern myself with returning a single matching document or multiple documents?

Here's what my user object looks like:

{
   "id": "e85ee3d7-44a5-4250-a116-686e1c2a10f5"
   "firstName": "Jane",
   "lastName": "Doe",
   "gender": "F"
}

And here's what my storedproc looks like so far:

function(gender) { 

   var context = getContext();
   var response = context.getResponse();
   var collection = context.getCollection();
   var collectionLink = collection.getSelfLink();

   var filterQuery = "SELECT * FROM c WHERE c.gender = '" + gender + "'";
   // Now what???
}

I'd like to return ALL female users. There may be one or 10,000 female users.

I'd appreciate your help with this simple DocumentDb stored procedure. Thanks.


回答1:


You're on the right track.

The next steps would be to run your filterQuery on the collection, and then place the query response in to the response variable.

For example:

function(gender) {
  var context = getContext();
  var response = context.getResponse();
  var collection = context.getCollection();
  var collectionLink = collection.getSelfLink();

  var filterQuery = 'SELECT * FROM c WHERE c.gender = "' + gender + '"';

  collection.queryDocuments(collectionLink, filterQuery, {},
    function(err, documents) {
      response.setBody(response.getBody() + JSON.stringify(documents));
    }
  );
}

You can find some more samples and documentation on stored procedures on DocumentDB's website: http://azure.microsoft.com/en-us/documentation/articles/documentdb-programming/



来源:https://stackoverflow.com/questions/26665767/simple-documentdb-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!