Reputation: 30388
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.
Upvotes: 4
Views: 5767
Reputation: 8119
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/
Upvotes: 6