Join Chander Dhall for an in-depth discussion in this video Trigger, part of Cosmos DB: Import, Manipulate, Index and Query.
In case of a post-trigger, any time the document is added and you want to trigger some functionality based on that addition, you could do that. So let's get rid of some of this code and see this in action. What we're going to do is, we're going to create a simple document and we're going to assign a pre-trigger to it, which will add the created date, as we just discussed. So we're going to have a static async function. And there's really no return type so it's going to be just a task.
And then we're going to pass in the same two things we always need, at least the collectionId and the databaseId. Well then we're going to get the collection, client.ReadDocumentCollectionAsync using the UriFactory createDocumentCollection Uri, and in this case, it'll be the databaseId and the collectionId.
And in this case, it will be scripts. A forwarder already exists, and then we can say AddCreatedDate.js. Well similar to what we did in case of a store proc and also the user-defined function, we're going to create a trigger and then we're going to have ID equals, let's do triggerID. And then we're going to have the body equals body.
And then we have the TriggerOperation equals TriggerOperation.Create. And the next thing would be TriggerType equals TriggerType.Pre. Well now, if the trigger actually exists, we want to make sure that we try to delete it before we move any further. And this is very similar to what we did in case of the store proc, as well as the udf, except that it will need the triggerId, which we have, and we're going to do control dot, and let Visual Studio create the method for us.
Just making sure we change this, so there is no ambiguity, I'm going to change this to trigger.Id. And then we do AsEnumerbale, and then FirstOrDefault. Now if this trigger is not null, same pattern as we used earlier that means this already exists, so we're going to delete it. We're going to use await client.DeleteTriggerAsync and pass in the self link to the trigger.
Now once we have deleted the trigger, we want to make sure that we create the actual trigger that we need. So in this case, it will be await client.CreateTriggerAsync, pass in the collection link, and then the entire trigger object that we just created. This needs to be the link. Well next thing to keep in mind is what kind of options we can send it.
We have requestOptions equals new RequestOptions. And then we can say PreTriggerInclude, in this case, we can pass in pretty much anything, we can pass in dynamic stuff, whatever we need. In this case, we're going to say list of string, and then we can say list triggerId. And we can also have PartitionKey. And I can call this PartitionKey whatever I want to call it, except make sure that it's consistent everywhere we use this PartitionKey.
And now it's time to create our document. We have our collectionLink, we want to create a document. And this is a complete dynamic document. So I could say, ID could be Quid.NewQuid. And I bet the ID happens to be a string or something, well it might work, but you could also do ToString. And then, if I remember some of the properties, another one was category_code.
And I'm going to call it temp, because that happens to be my partition key. And then there are a few other properties. For example, we have homepage URL, and that could be cazton.com. And we have name equals Cazton and then pass in the request options. Well next, what we want to do is, after we created this document, we want this document to come from the Cosmos DB instance of Azure.
Just to make this better, we can also call it Company, because the category code could be something like a company. In our case we had web, advertising, a lot of others. It could also be a department. I don't want to use sales or anything, because it could already exist. So I'm just giving it a name that's completely separate, so if we actually make a query for the category code, we can get the data back as it is. So now we have var results, we can say client.CreateDocumentQuery, because we're trying to get the Caztan document back from Cosmos DB.
So in this case, it's going to be a query. And a query would need a collectionLink. As you notice, it's a very similar API as what we've been using. And now we can say, from root r where r.category_code, and in this case, the category_code happens to be company. We could have also done r.name as Caztan, or anything similar, any kind of query you could have gotten, you could have even gone for the homepage URL, or even the ID, everything should work.
Because by default, document db indexes everything automatically. And now we have FeedOptions. Remember this part is important because we are going to do the query, that will be a cross-partition query, but we need to make sure that we enable this. And then close the curly brackets, that's it. Well now that we have the results, we can choose to foreach through the results. And I could literally pick the value here and display them one by one, if I needed.
So now we've created a new property inside the document, and since this is a pre-trigger, the document will be created with this new property, which is giving you the time of creation of the document. So once we're done with this, the next thing we have to do is make sure that we set the body of the request to the new document, which has an appended field, which is createdTime.
And it's as simple as that. Go back to program.cs, all the way up, and then call this particular function. Another thing you want to make sure is right-click on this, and change this to copy if newer. Create a date, databaseId is Cazton, and then collectionId would be anything. Since we already created something called Company, we can say Company and say Wait.
I want to put a break point right here, so I can see the result that comes in, and say F5, or maybe just remove the break point and do control F5 so we can see it in the terminal. So as you can see, not only that we get the entire object, we also have something called createdTime, which was actually added by the pre-trigger that we just created.
- 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