Skip to content

Instantly share code, notes, and snippets.

@joeriks
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeriks/d15a2d87fcc3ccfc6635 to your computer and use it in GitHub Desktop.
Save joeriks/d15a2d87fcc3ccfc6635 to your computer and use it in GitHub Desktop.
Experimenting with DocumentDb - basic write and read (from ScottGu example) - and timing from local pc
Time to create connection and get collection 2513 ms
Time to perform simple query 448 ms
Andersen family:
{
"id": "AndersenFamily",
"lastName": "Andersen",
"parents": [
{
"firstName": "Thomas"
},
{
"firstName": "Mary Kay"
}
],
"children": [
{
"firstName": "John",
"gender": "male",
"grade": 7
}
],
"pets": [
{
"givenName": "Fluffy"
}
],
"address": {
"country": "USA",
"state": "WA",
"city": "Seattle"
},
"_rid": ...
"_attachments": "attachments/"
}
Time to convert and print 13 ms
Time for next query 449 ms
First male child:
{
"firstName": "John",
"gender": "male",
"grade": 7
}
Time to convert and print 2 ms
using (var client = new DocumentClient(new Uri(endpoint), authKey))
{
var s = Stopwatch.StartNew();
var database = client.CreateDatabaseQuery().Where (c => c.Id=="AnotherDb").AsEnumerable().Single ();
var collection = client.CreateDocumentCollectionQuery(database.SelfLink).Where (c=>c.Id=="Families").AsEnumerable().Single ( );
Console.WriteLine("Time to create connection and get collection {0} ms",s.ElapsedMilliseconds);
s.Restart();
var query = client.CreateDocumentQuery(collection.SelfLink, "SELECT * FROM Families f WHERE f.id = 'AndersenFamily'");
var family = query.AsEnumerable().Single ();
Console.WriteLine("Time to perform simple query {0} ms",s.ElapsedMilliseconds);
s.Restart();
Console.WriteLine("Andersen family:");
Console.WriteLine(((JObject)family).ToString());
Console.WriteLine("Time to convert and print {0} ms",s.ElapsedMilliseconds);
s.Restart();
query = client.CreateDocumentQuery(collection.DocumentsLink, "SELECT * FROM c IN Families.children WHERE c.gender='male'");
var child = query.AsEnumerable().FirstOrDefault();
Console.WriteLine("Time for next query {0} ms",s.ElapsedMilliseconds);
s.Restart();
Console.WriteLine("First male child:");
Console.WriteLine(((JObject)child).ToString());
Console.WriteLine("Time to convert and print {0} ms",s.ElapsedMilliseconds);
}
using (var client = new DocumentClient(new Uri(endpoint), authKey))
{
var database = new Database { Id = "AnotherDb" };
database = await client.CreateDatabaseAsync(database);
var collection = new DocumentCollection { Id = "Families" };
collection = await client.CreateDocumentCollectionAsync(database.SelfLink, collection);
dynamic andersonFamily = JsonConvert.DeserializeObject(@"
{
'id': 'AndersenFamily',
'lastName': 'Andersen',
'parents': [
{ 'firstName': 'Thomas' },
{ 'firstName': 'Mary Kay' }
],
'children': [
{ 'firstName': 'John', 'gender': 'male', 'grade': 7 }
],
'pets': [
{ 'givenName': 'Fluffy' }
],
'address': { 'country': 'USA', 'state': 'WA', 'city': 'Seattle' }
}");
dynamic wakefieldFamily = JsonConvert.DeserializeObject(@"
{
'id': 'WakefieldFamily',
'parents': [
{ 'familyName': 'Wakefield', 'givenName': 'Robin' },
{ 'familyName': 'Miller', 'givenName': 'Ben' }
],
'children': [
{
'familyName': 'Wakefield',
'givenName': 'Jesse',
'gender': 'female',
'grade': 1
},
{
'familyName': 'Miller',
'givenName': 'Lisa',
'gender': 'female',
'grade': 8
}
],
'pets': [
{ 'givenName': 'Goofy' },
{ 'givenName': 'Shadow' }
],
'address': { 'country': 'USA', 'state': 'NY', 'county': 'Manhattan', 'city': 'NY' }
}"
);
await client.CreateDocumentAsync(collection.SelfLink, andersonFamily);
await client.CreateDocumentAsync(collection.SelfLink, wakefieldFamily);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment