Last active
May 31, 2018 11:21
-
-
Save aheumaier/8f108f0d34510864f0c32ce23ef08efc to your computer and use it in GitHub Desktop.
CosmosDB Aggreates Pipeline Abilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# query sammple on aggregation | |
require 'mongo' | |
client = Mongo::Client.new('mongodb://mymongosample:MYSECRET==@mymongosample.documents.azure.com:10255/debugSample?ssl=true&replicaSet=globaldb') | |
collection = client[:Hazard] | |
# Yields a BSON::Document. | |
def output(data) | |
puts 'Found ' + data.count.to_s + ' documents' | |
data.to_a[0..3].each do |doc| | |
puts doc | |
end | |
puts '' | |
puts '>>>' | |
puts '' | |
end | |
# case 1 | |
# solution: _id is not a string but an int | |
aggregation_1 = collection.aggregate([ | |
{ '$match' => { '_id' => { '$in' => [434, 445] } } } | |
]) | |
output aggregation_1 | |
# works also with strings | |
aggregation_1a = collection.aggregate([ | |
{ '$match' => { 'tname' => { '$in' => %w[iter434 iter445] } } } | |
]) | |
output aggregation_1a | |
# case 2 | |
# nested pipeline operator are not possible right now. Migitate $in, $regex with $or | |
aggregation_2 = collection.aggregate([ | |
{ '$match' => { '$or' => [{ 'groups._id' => 123 }, { 'groups._id' => 789 }] } } | |
]) | |
output aggregation_2 | |
# case 3 | |
# see case 2 | |
aggregation_3 = collection.aggregate([ | |
{ '$match' => { 'tname' => { '$regex' => '^iter1' } } } | |
]) | |
output aggregation_3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# database filler helper | |
# just gem install mongo | |
require 'mongo' | |
# setup database client | |
client = Mongo::Client.new('mongodb://mymongosample:MYSECRET==@mymongosample.documents.azure.com:10255/debugSample?ssl=true&replicaSet=globaldb') | |
db = client.database | |
collection = client[:Hazard] | |
# data sample structure | |
# { | |
# "_id": "123", | |
# "groups": [{ | |
# "_id": "456", | |
# "name": "Group 2" | |
# }, { | |
# "_id": "789", | |
# "name": "Group 2" | |
# }] | |
# } | |
# add same sample subgroups | |
$groups = [{"_id" => 123, "name" => "Group 1"}, | |
{"_id" => 456, "name" => "Group 2"}, | |
{"_id" => 789, "name" => "Group 3"}] | |
$docs = [] | |
# generate 10.000 documents with random groups from above | |
10000.times do |iter| | |
$docs << {"_id" => iter, "tname" => "iter"+iter.to_s, "groups" => $groups.sample(2) } | |
end | |
# add docs in batch fashion to cosmosDB | |
result = collection.insert_many($docs) | |
puts result.inserted_count # returns "10.000 " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment