Exemple #1
0
        /// <summary>
        /// Create a post trigger that updates metadata: for each inserted doc it will look at doc.size
        /// and update aggregate properties: { minSize, maxSize, totalSize } in the metadata doc.
        /// In the end print to show the aggregate values of min, max, total for all docs.
        /// </summary>
        private static async Task RunPostTrigger(string collectionLink)
        {
            Random rnd = new Random();

            // 1. Create a trigger.
            string  triggerPath = @"js\UpdateMetadata.js";
            string  triggerId   = Path.GetFileNameWithoutExtension(triggerPath);
            string  triggerBody = File.ReadAllText(triggerPath);
            Trigger trigger     = new Trigger
            {
                Id               = Path.GetFileName(triggerId),
                Body             = triggerBody,
                TriggerOperation = TriggerOperation.Create,
                TriggerType      = TriggerType.Post
            };

            await TryDeleteTrigger(collectionLink, trigger.Id);

            await client.CreateTriggerAsync(collectionLink, trigger);

            // 2. Create the metadata document.
            var aggregateEntry = new LoggingAggregateEntry
            {
                Id         = "Device001_metadata",
                IsMetadata = true,
                MinSize    = 0,
                MaxSize    = 0,
                TotalSize  = 0,
                DeviceId   = "Device001"
            };

            await client.CreateDocumentAsync(collectionLink, aggregateEntry);

            // 3. Import a number of docs with trigger. Use client API this time, we already have sample fot using script.
            var requestOptions = new RequestOptions {
                PostTriggerInclude = new List <string> {
                    triggerId
                }
            };

            for (int i = 0; i < 4; i++)
            {
                await client.CreateDocumentAsync(
                    collectionLink,
                    new LoggingEntry { DeviceId = "Device001", Size = rnd.Next(1000) },
                    requestOptions);
            }

            // 4. Print aggregate info from the metadata document.
            aggregateEntry = client.CreateDocumentQuery <dynamic>(
                collectionLink,
                "SELECT * FROM root r WHERE r.isMetadata = true",
                new FeedOptions {
                PartitionKey = new PartitionKey("Device001")
            }).AsEnumerable().First();

            Console.WriteLine("Document statistics: min size: {0}, max size: {1}, total size: {2}",
                              aggregateEntry.MinSize,
                              aggregateEntry.MaxSize,
                              aggregateEntry.TotalSize);
        }
        /// <summary>
        /// Create a post trigger that updates metadata: for each inserted doc it will look at doc.size
        /// and update aggregate properties: { minSize, maxSize, totalSize } in the metadata doc.
        /// In the end print to show the aggregate values of min, max, total for all docs.
        /// </summary>
        private static async Task RunPostTrigger(string collectionLink)
        {
            Random rnd = new Random();

            // 1. Create a trigger.
            string triggerPath = @"js\UpdateMetadata.js";
            string triggerId = Path.GetFileNameWithoutExtension(triggerPath);
            string triggerBody = File.ReadAllText(triggerPath);
            Trigger trigger = new Trigger
            {
                Id = Path.GetFileName(triggerId),
                Body = triggerBody,
                TriggerOperation = TriggerOperation.Create,
                TriggerType = TriggerType.Post
            };

            await TryDeleteTrigger(collectionLink, trigger.Id);
            await client.CreateTriggerAsync(collectionLink, trigger);

            // 2. Create the metadata document.
            var aggregateEntry = new LoggingAggregateEntry
            {
                Id = "Device001_metadata",
                IsMetadata = true,
                MinSize = 0,
                MaxSize = 0,
                TotalSize = 0,
                DeviceId = "Device001"
            };

            await client.CreateDocumentAsync(collectionLink, aggregateEntry); 
            
            // 3. Import a number of docs with trigger. Use client API this time, we already have sample fot using script.
            var requestOptions = new RequestOptions { PostTriggerInclude = new List<string> { triggerId } };

            for (int i=0; i < 4; i++)
            {
                await client.CreateDocumentAsync(
                    collectionLink,
                    new LoggingEntry { DeviceId = "Device001", Size = rnd.Next(1000) },
                    requestOptions);
            }

            // 4. Print aggregate info from the metadata document.
            aggregateEntry = client.CreateDocumentQuery<dynamic>(
                collectionLink, 
                "SELECT * FROM root r WHERE r.isMetadata = true",
                new FeedOptions { PartitionKey = new PartitionKey("Device001") }).AsEnumerable().First();

            Console.WriteLine("Document statistics: min size: {0}, max size: {1}, total size: {2}", 
                aggregateEntry.MinSize, 
                aggregateEntry.MaxSize, 
                aggregateEntry.TotalSize);
        }