Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created August 22, 2014 07:31
Show Gist options
  • Select an option

  • Save joeriks/bb2c355be386ff3b75ad to your computer and use it in GitHub Desktop.

Select an option

Save joeriks/bb2c355be386ff3b75ad to your computer and use it in GitHub Desktop.
Test DocumentDb from trywebsites.azurewebsites.net
Time to create connection and get collection 671
Time to perform simple query 126
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":"==","_ts":1408692077,"_self":"dbs/8qgSAA==/colls/=/docs/==/","_etag":"00000c00-0000-0000-0000-53f6ef6d0000","_attachments":"attachments/"}
Time to convert and print 0
Time for next query 104
First male child:
{"firstName":"John","gender":"male","grade":7}
Time to convert and print 0
@using Microsoft.Azure.Documents
@using Microsoft.Azure.Documents.Client
@using Microsoft.Azure.Documents.Linq
@using Newtonsoft.Json
@using Newtonsoft.Json.Bson
@using Newtonsoft.Json.Converters
@using Newtonsoft.Json.Linq
@using Newtonsoft.Json.Schema
@using Newtonsoft.Json.Serialization
@using System.Threading.Tasks
@{
string endpoint = x;
string authKey = y;
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' }
}");
using (var client = new DocumentClient(new Uri(endpoint), authKey))
{
var database = new Microsoft.Azure.Documents.Database { Id = "FromAzure" };
database = client.CreateDatabaseAsync(database).Result;
var collection = new DocumentCollection { Id = "Families" };
collection = client.CreateDocumentCollectionAsync(database.SelfLink, collection).Result;
client.CreateDocumentAsync(collection.SelfLink, andersonFamily).Wait();
client.CreateDocumentAsync(collection.SelfLink, wakefieldFamily).Wait();
}
}
<p>Done</p>
@using Microsoft.Azure.Documents
@using Microsoft.Azure.Documents.Client
@using Microsoft.Azure.Documents.Linq
@using Newtonsoft.Json
@using Newtonsoft.Json.Bson
@using Newtonsoft.Json.Converters
@using Newtonsoft.Json.Linq
@using Newtonsoft.Json.Schema
@using Newtonsoft.Json.Serialization
@using System.Threading.Tasks
@using System.Diagnostics
@{
string endpoint = x;
string authKey = y;
using (var client = new DocumentClient(new Uri(endpoint), authKey))
{
var s = Stopwatch.StartNew();
var database = client.CreateDatabaseQuery().Where (c => c.Id=="FromAzure").AsEnumerable().Single ();
var collection = client.CreateDocumentCollectionQuery(database.SelfLink).Where (c=>c.Id=="Families").AsEnumerable().Single ( );
<p>Time to create connection and get collection @s.ElapsedMilliseconds</p>
s.Restart();
var query = client.CreateDocumentQuery(collection.SelfLink, "SELECT * FROM Families f WHERE f.id = 'AndersenFamily'");
var family = query.AsEnumerable().Single ();
<p>Time to perform simple query @s.ElapsedMilliseconds</p>
s.Restart();
<p>Andersen family:</p>
<p>@family</p>
<p>Time to convert and print @s.ElapsedMilliseconds</p>
s.Restart();
query = client.CreateDocumentQuery(collection.DocumentsLink, "SELECT * FROM c IN Families.children WHERE c.gender='male'");
var child = query.AsEnumerable().FirstOrDefault();
<p>Time for next query @s.ElapsedMilliseconds</p>
s.Restart();
<p>First male child:</p>
<p>@child</p>
<p>Time to convert and print @s.ElapsedMilliseconds</p>
}
}
@joeriks
Copy link
Author

joeriks commented Aug 22, 2014

How To:

1 Create the DocumentDb in your Azure portal
2 Create a sample site in https://trywebsites.azurewebsites.net/
3 Choose Hello C#
4 Open it in Visual Studio online
5 Open the Console (third icon in the left bar)
6 Install nuget with the command install package Microsoft.Azure.Documents.Client -Pre
7 It adds folders for Azure.Documents and Newtonsoft.Json
8 move the .dll file from Micsoroft.Azure.Documents/lib/ to /bin/
9 move the .dll file from Newtonsoft.Json/lib/net40/ to /bin/
10 create CreateDb.cshtml, paste the code above, paste your uri and key
11 create ReadFromDb.cshtml, paste the code above, paste your uri and key
12 run the site - go to url /CreateDb
13 go to url /ReadFromDb

@joeriks
Copy link
Author

joeriks commented Aug 22, 2014

Down to ~20ms now - when I managed to get the db in the same resourcegroup (a regular Azure website project) - http://joeriks-playit.azurewebsites.net/TryReadFromDocumentDb

@joeriks
Copy link
Author

joeriks commented Aug 22, 2014

Another note - changing configuration (for example to eventual consistency) takes very long time (over 10 minutes)

@joeriks
Copy link
Author

joeriks commented Aug 22, 2014

So does creating a new documentdb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment