@@ -20,8 +20,6 @@ namespace DocumentDB.GetStarted
20
20
using Microsoft . Azure . Documents ;
21
21
using Microsoft . Azure . Documents . Client ;
22
22
using Microsoft . Azure . Documents . Linq ;
23
-
24
- // Add Newtonsoft.Json references
25
23
using Newtonsoft . Json ;
26
24
27
25
/// <summary>
@@ -61,44 +59,47 @@ public static void Main(string[] args)
61
59
62
60
private static async Task GetStartedDemo ( )
63
61
{
64
- // Make sure to call client.Dispose() once you've finished all DocumentDB interactions
65
62
// Create a new instance of the DocumentClient
66
63
var client = new DocumentClient ( new Uri ( EndpointUrl ) , AuthorizationKey ) ;
67
64
68
65
// Check to verify a database with the id=FamilyRegistry does not exist
69
66
Database database = client . CreateDatabaseQuery ( ) . Where ( db => db . Id == "FamilyRegistry" ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
70
67
68
+ // If the database does not exist, create a new database
71
69
if ( database == null )
72
70
{
73
- // Create a database
74
71
database = await client . CreateDatabaseAsync (
75
72
new Database
76
73
{
77
74
Id = "FamilyRegistry"
78
75
} ) ;
76
+
77
+ WriteMessage ( "Created dbs/FamilyRegistry" ) ;
79
78
}
80
- else { Warn ( "database" ) ; }
81
79
82
80
// Check to verify a document collection with the id=FamilyCollection does not exist
83
- DocumentCollection documentCollection = client . CreateDocumentCollectionQuery ( database . CollectionsLink ) . Where ( c => c . Id == "FamilyCollection" ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
81
+ DocumentCollection documentCollection = client . CreateDocumentCollectionQuery ( "dbs/" + database . Id ) . Where ( c => c . Id == "FamilyCollection" ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
84
82
83
+ // If the document collection does not exist, create a new collection
85
84
if ( documentCollection == null )
86
85
{
87
- // Create a document collection using the lowest performance tier available (currently, S1)
88
- documentCollection = await client . CreateDocumentCollectionAsync ( database . CollectionsLink ,
89
- new DocumentCollection { Id = "FamilyCollection" } ,
90
- new RequestOptions { OfferType = "S1" } ) ;
86
+ documentCollection = await client . CreateDocumentCollectionAsync ( "dbs/" + database . Id ,
87
+ new DocumentCollection
88
+ {
89
+ Id = "FamilyCollection"
90
+ } ) ;
91
+
92
+ WriteMessage ( "Created dbs/FamilyRegistry/colls/FamilyCollection" ) ;
91
93
}
92
-
93
- else { Warn ( "document collection" ) ; }
94
94
95
95
// Check to verify a document with the id=AndersenFamily does not exist
96
- Document document = client . CreateDocumentQuery ( documentCollection . DocumentsLink ) . Where ( d => d . Id == "AndersenFamily" ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
96
+ Document document = client . CreateDocumentQuery ( "dbs/" + database . Id + "/colls/" + documentCollection . Id ) . Where ( d => d . Id == "AndersenFamily" ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
97
97
98
+ // If the document does not exist, create a new document
98
99
if ( document == null )
99
100
{
100
101
// Create the Andersen Family document
101
- Family AndersonFamily = new Family
102
+ Family andersonFamily = new Family
102
103
{
103
104
Id = "AndersenFamily" ,
104
105
LastName = "Andersen" ,
@@ -121,17 +122,18 @@ private static async Task GetStartedDemo()
121
122
IsRegistered = true
122
123
} ;
123
124
124
- await client . CreateDocumentAsync ( documentCollection . DocumentsLink , AndersonFamily ) ;
125
+ await client . CreateDocumentAsync ( "dbs/" + database . Id + "/colls/" + documentCollection . Id , andersonFamily ) ;
126
+
127
+ WriteMessage ( "Created dbs/FamilyRegistry/colls/FamilyCollection/docs/AndersenFamily" ) ;
125
128
}
126
- else { Warn ( "document" ) ; }
127
129
128
130
// Check to verify a document with the id=AndersenFamily does not exist
129
- document = client . CreateDocumentQuery ( documentCollection . DocumentsLink ) . Where ( d => d . Id == "WakefieldFamily" ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
131
+ document = client . CreateDocumentQuery ( "dbs/" + database . Id + "/colls/" + documentCollection . Id ) . Where ( d => d . Id == "WakefieldFamily" ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
130
132
131
133
if ( document == null )
132
134
{
133
135
// Create the WakeField document
134
- Family WakefieldFamily = new Family
136
+ Family wakefieldFamily = new Family
135
137
{
136
138
Id = "WakefieldFamily" ,
137
139
Parents = new Parent [ ] {
@@ -160,13 +162,13 @@ private static async Task GetStartedDemo()
160
162
IsRegistered = false
161
163
} ;
162
164
163
- await client . CreateDocumentAsync ( documentCollection . DocumentsLink , WakefieldFamily ) ;
164
- }
165
- else { Warn ( "document" ) ; }
165
+ await client . CreateDocumentAsync ( "dbs/" + database . Id + "/colls/" + documentCollection . Id , wakefieldFamily ) ;
166
166
167
+ WriteMessage ( "Created dbs/FamilyRegistry/colls/FamilyCollection/docs/WakefieldFamily" ) ;
168
+ }
167
169
168
170
// Query the documents using DocumentDB SQL for the Andersen family
169
- var families = client . CreateDocumentQuery ( documentCollection . DocumentsLink ,
171
+ var families = client . CreateDocumentQuery ( "dbs/" + database . Id + "/colls/" + documentCollection . Id ,
170
172
"SELECT * " +
171
173
"FROM Families f " +
172
174
"WHERE f.id = \" AndersenFamily\" " ) ;
@@ -178,17 +180,17 @@ private static async Task GetStartedDemo()
178
180
179
181
// Query the documents using LINQ for the Andersen family
180
182
families =
181
- from f in client . CreateDocumentQuery ( documentCollection . DocumentsLink )
183
+ from f in client . CreateDocumentQuery ( "dbs/" + database . Id + "/colls/" + documentCollection . Id )
182
184
where f . Id == "AndersenFamily"
183
185
select f ;
184
186
185
187
foreach ( var family in families )
186
188
{
187
- Console . WriteLine ( "\t Read {0} from LINQ" , family ) ;
189
+ Console . WriteLine ( "Read {0} from LINQ" , family ) ;
188
190
}
189
191
190
192
// Query the documents using LINQ lambdas for the Andersen family
191
- families = client . CreateDocumentQuery ( documentCollection . DocumentsLink )
193
+ families = client . CreateDocumentQuery ( "dbs/" + database . Id + "/colls/" + documentCollection . Id )
192
194
. Where ( f => f . Id == "AndersenFamily" )
193
195
. Select ( f => f ) ;
194
196
@@ -197,40 +199,15 @@ from f in client.CreateDocumentQuery(documentCollection.DocumentsLink)
197
199
Console . WriteLine ( "\t Read {0} from LINQ query" , family ) ;
198
200
}
199
201
200
- // Query the documents using DocumentSQl with one join
201
- var items = client . CreateDocumentQuery < dynamic > ( documentCollection . DocumentsLink ,
202
- "SELECT f.id, c.FirstName AS child " +
203
- "FROM Families f " +
204
- "JOIN c IN f.Children" ) ;
205
-
206
- foreach ( var item in items . ToList ( ) )
207
- {
208
- Console . WriteLine ( item ) ;
209
- }
210
-
211
- // Query the documents using LINQ with one join
212
- items = client . CreateDocumentQuery < Family > ( documentCollection . DocumentsLink )
213
- . SelectMany ( family => family . Children
214
- . Select ( children => new
215
- {
216
- family = family . Id ,
217
- child = children . FirstName
218
- } ) ) ;
219
-
220
- foreach ( var item in items . ToList ( ) )
221
- {
222
- Console . WriteLine ( item ) ;
223
- }
224
-
225
202
// Clean up/delete the database and client
226
- await client . DeleteDatabaseAsync ( database . SelfLink ) ;
203
+ await client . DeleteDatabaseAsync ( "dbs/" + database . Id ) ;
227
204
client . Dispose ( ) ;
228
205
}
229
206
230
- private static void Warn ( string resource )
207
+ private static void WriteMessage ( string msg )
231
208
{
232
- Console . WriteLine ( "Warning: A " + resource + " with the same id already exists" ) ;
233
- Console . WriteLine ( "Continuing may modify the existing " + resource + ". \n Press any key to continue.") ;
209
+ Console . WriteLine ( msg ) ;
210
+ Console . WriteLine ( "Press any key to continue .. ." ) ;
234
211
Console . ReadKey ( ) ;
235
212
Console . Clear ( ) ;
236
213
}
0 commit comments