Join Chander Dhall for an in-depth discussion in this video Import documents with stored procedure, part of Cosmos DB: Import, Manipulate, Index and Query.
It could be an array of documents, or it could be just one document, but it's passed in as an array. We already have the context from that context. I can also get the collection link. One easy way to do this would be var collectionLink equals collection.getSelfLink. Once I have that, I want to keep track of the count. I can say var count is zero. If my input is invalid, I'd like to check for it.
In that case, I would like to set the response like what we did in the case of a simple query. We set the body, however, I'm just going to set the body to zero. And then, next, I'd like to create these documents. In order to do that, I'll take that particular document that I want to create, pass it over, and it will come back with a callback. And I don't really need any of this code, so I'm going to get rid of all this.
So tryCreate function is a function that actually works on every single document, one by one. So I'll have the document and then I'll have the callback. One of the things to keep in mind is what kind of options we have. Let's assume that we have a file, like what we had previously, which was company start json. As we know, we had IDs as part of the document. Well what we could do, in order to improve performance, is use something like disableAutomaticIdGeneration, and say true.
The advantage of this is that document db is now not going to parse your documents, to look for an ID. And that will speed up your request. And then I'll go ahead and create the document. And to create the document, I definitely need the collectionLink. And as you can see, I need the document, of course, the options, and then also the callback.
And if for some reasons, the call isn't accepted, the request that we made doesn't go through, for some reasons, then we'll make sure that we set the response, in this case, using setBody to just the count. Now the only thing left to implement is going to be the callback function. The callback function, as always, the first parameter is an error, the second one is going to be the document, and then we have the options.
In this case, we're only concerned with the error. So we'll say, if there's an error, throw that particular error. And if there is no error, well, that means everything went through, so we do a count plus plus, and then make sure, if the count is greater than or equals jsonCount, which was the count of the document, well in that case, make sure we use setBody to send the count back in the response.
Otherwise, we create the next document. In this case, it'll be jsonArray of count, and then the callback.
- Data import scenarios
- Creating a database
- Creating a partitioned collection
- Data manipulation
- Importing documents with a stored procedure
- User-defined functions
- Excluding indexing at a document level
- Range indexing on strings
- Querying with SQL parameters
- Range operations