MongoExtensions / src / MongoGui / OldStuff.cs
Code · 512 lines · 12444 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512using MongoDB.Bson;
using MongoDB.Driver;
using MongoExtensions;


namespace MongoGui;

internal class OldStuff
{
	private static readonly MongoClient dbClient = new("mongodb://192.168.28.210:27017");

	private static async Task TransactionTest()
	{
		if (dbClient != null)
		{
			using var iSession = await dbClient.StartSessionAsync();
			iSession.StartTransaction();
			try
			{
				// Deletes Updates Inserts
				await iSession.CommitTransactionAsync();
			}
			catch
			{
				await iSession.AbortTransactionAsync();
			}
		}
	}


	private static async Task Test0()
	{
		Console.WriteLine("The list of databases on this server is: ");
		Console.WriteLine(dbClient?.ListDatabases().ToList().Pretty());

		List<BsonDocument> result;

		var db = dbClient?.GetDatabase("acme");

		//await db.CreateCollectionAsync("Alphons");

		var alphons = db?.GetCollection("Alphons");

		//var objectid = await alphons.InsertOneAsync(new { Name = "Alphonsje 2", Age = 55 });

		Console.WriteLine(alphons?.Find("{}").ToList().Pretty());


		Console.WriteLine("The list of collections on database acme: ");
		Console.WriteLine(db?.ListCollections().Pretty());

		var collection = db?.GetCollection("posts");

		var col = dbClient?.GetDatabase("sample_training").GetCollection("grades");

		var goodscores = await col.Find(@"
				{
					scores: 
					{
						$elemMatch:
						{
							type: 'exam',
							score : { '$gte': 89 }
						}
					}
				}")
			.ToListAsync();

		Console.WriteLine(goodscores.Pretty());


		//var delResult11 = await col.DeleteOneAsync("{ 'student_id': 10001 }");

		var document = @"
			{
				'student_id': 10006,
				'scores': 
					[
						{ 'type': 'exam' , 'score': 89.12334193287023 } ,
						{ 'type': 'quiz' , 'score': 74.92381029342834 } ,
						{ 'type': 'homework' , 'score': 89.97929384290324 } ,
						{ 'type': 'homework' , 'score': 82.12931030513218 } 
					],
				'class_id': 480
			}";

		if (col != null)
		{
			var id = await col.InsertOneAsync(document);
			Console.WriteLine("Id = " + id);
		}

		var documents = @"[
			{
				'student_id': 10007,
				'scores': 
					[
						{ 'type': 'exam' , 'score': 88.12334193287023 } ,
						{ 'type': 'quiz' , 'score': 74.92381029342834 } ,
						{ 'type': 'homework' , 'score': 89.97929384290324 } ,
						{ 'type': 'homework' , 'score': 82.12931030513218 } 
					],
				'class_id': 480
			},
			{
				'student_id': 10008,
				'scores': 
					[
						{ 'type': 'exam' , 'score': 88.12334193287023 } ,
						{ 'type': 'quiz' , 'score': 74.92381029342834 } ,
						{ 'type': 'homework' , 'score': 89.97929384290324 } ,
						{ 'type': 'homework' , 'score': 82.12931030513218 } 
					],
				'class_id': 480
			}]";

		if (col != null)
		{
			var ids = await col.InsertManyAsync(documents);

			Console.WriteLine(string.Join(',', ids));
		}

		Console.Write(col.Find("{}").ToList().Pretty());


		var filter = Builders<BsonDocument>.Filter.Eq("student_id", 10000);
		var update = Builders<BsonDocument>.Update.Set("class_id", 483);

		var updResult = col?.UpdateOne(filter, update);

		var x = 486;
		if (col != null)
		{
			var updResult2 = await col.UpdateOneAsync("{ student_id : 10000 }", $"{{ $set: {{ class_id : {x} }} }}");
		}

		if (collection != null)
		{
			var count = await collection.CountDocumentsAsync("{}");
		}

		var aaaa = new { views = new { _gt = 2 } };

		var cc = await collection
			.Find("{ views: { $gt: 2 } }").CountDocumentsAsync();

		result = await collection
			.Find("{ views: { $gt: 2 } }")
			.Project("{ _id:0, title: 1, date: 1, views: 1 }")
			.Sort("{ _id: -1 }")
			.ToListAsync();

		Console.WriteLine(result.Pretty());

		result = await collection
			.Find(@"
				{
					comments: 
					{
						$elemMatch:
						{
							user: 'Mary Williams'
						}
					}
				}")
			.ToListAsync();

		Console.WriteLine(result.Pretty());

		if (collection != null)
		{
			var result3 = await collection.UpdateOneAsync("{ title: 'Post Two' }", @"
{
	$set: 
	{
		body: 'Body for post 2222',
		category: 'Technology'
	}
}");
			var mac = result3.MatchedCount;
			var moc = result3.ModifiedCount;
		}



		//var highExamScoreFilter = Builders<BsonDocument>.Filter.ElemMatch<BsonValue>(
		//		"scores", new BsonDocument { 
		//			{ "type", "exam" },
		//			{ "score", new BsonDocument { { "$gte", 88 } } } });

		//var highExamScores = collection.Find(highExamScoreFilter).ToList();
		//var cursor = collection.Find(highExamScoreFilter).ToCursor();
		//foreach (var document in cursor.ToEnumerable())
		//{
		//	Console.WriteLine(document);
		//}

		//await collection.Find(highExamScoreFilter).ForEachAsync(document => Console.WriteLine(document));

		//var sort = Builders<BsonDocument>.Sort.Descending("student_id");

		//var highestScores = collection.Find(highExamScoreFilter).Sort(sort);

		//var highestScore = collection.Find(highExamScoreFilter).Sort(sort).First();

		//Console.WriteLine(highestScore);


		//var filter = Builders<BsonDocument>.Filter.Eq("student_id", 10000);
		//var update = Builders<BsonDocument>.Update.Set("class_id", 483);

		//collection.UpdateOne(filter, update);

		//var arrayFilter = Builders<BsonDocument>.Filter.Eq("student_id", 10000)
		//				& Builders<BsonDocument>.Filter.Eq("scores.type", "quiz");

		//var arrayUpdate = Builders<BsonDocument>.Update.Set("scores.$.score", 84.92381029342834);

		//collection.UpdateOne(arrayFilter, arrayUpdate);

		//var deleteFilter = Builders<BsonDocument>.Filter.Eq("student_id", 10000);

		//collection.DeleteOne(deleteFilter);

		//var deleteLowExamFilter = Builders<BsonDocument>.Filter.ElemMatch<BsonValue>("scores",
		//		new BsonDocument { { "type", "exam" }, {"score", new BsonDocument { { "$lt", 60 }}}});

		//collection.DeleteMany(deleteLowExamFilter);

		//var document = new BsonDocument 
		//{ 
		//	{ 
		//		"student_id", 10000 
		//	}, 
		//	{
		//		"scores", new BsonArray
		//		{
		//			new BsonDocument { { "type", "exam" }, { "score", 88.12334193287023 } },
		//			new BsonDocument { { "type", "quiz" }, { "score", 74.92381029342834 } },
		//			new BsonDocument { { "type", "homework" }, { "score", 89.97929384290324 } },
		//			new BsonDocument { { "type", "homework" }, { "score", 82.12931030513218 } }
		//		}
		//	}, 
		//	{ 
		//		"class_id", 480 
		//	}
		//};
		//await collection.InsertOneAsync(document);

	}

	private static async Task Test1()
	{
		var forecast = @"
{
  _id: 2,
  title: '123 Department Report',
  tags: [ 'G', 'STLW' ],
  year: 2014,
  subsections: [
    {
      subtitle: 'Section 1: Overview',
      tags: [ 'SI', 'G' ],
      content:  'Section 1: This is the content of section 1.'
    },
    {
      subtitle: 'Section 2: Analysis',
      tags: [ 'STLW' ],
      content: 'Section 2: This is the content of section 2.'
    },
    {
      subtitle: 'Section 3: Budgeting',
      tags: [ 'TK' ],
      content: {
        text: 'Section 3: This is the content of section3.',
        tags: [ 'HCS' ]
      }
    }
  ]
}
";
		var forecasts = dbClient?.GetDatabase("test1").GetCollection("forecasts");

		if (forecasts == null)
			return;

		if (forecasts.CountDocuments() == 0)
		{
			var id = await forecasts.InsertOneAsync(forecast);
		}

		var userAccess = "[ 'STLW', 'G' ]";

		var result = await forecasts.AggregateAsync(@"
	[
     { $match: { year: 2014 } },
     { $redact: {
        $cond: {
           if: { $gt: [ { $size: { $setIntersection: [ '$tags', " + userAccess + @" ] } }, 0 ] },
           then: '$$DESCEND',
           else: '$$PRUNE'
         }
       }
     }
	]");

		Console.WriteLine(result.Pretty());
	}

	private static async Task Test2()
	{
		var accounts = dbClient?.GetDatabase("test1").GetCollection("accounts");

		if (accounts == null)
			return;

		//Console.WriteLine(accounts.Pretty());

		var account = @"
{
  _id: 3,
  level: 1,
  acct_id: 'xyz123',
  cc: {
    level: 5,
    type: 'yy',
    num: 0,
    exp_date: ISODate('2015-11-01T00:00:00.000Z'),
    billing_addr: {
      level: 5,
      addr1: '123 ABC Street',
      city: 'Some City'
    },
    shipping_addr: [
      {
        level: 6,
        addr1: '987 XYZ Ave',
        city: 'Some City'
      },
      {
        level: 5,
        addr1: 'PO Box 0123',
        city: 'Some City'
      }
    ]
  },
  status: 'A'
}";
		if (accounts.CountDocuments() == 2)
		{
			var id = await accounts.InsertOneAsync(account);
		}

		var result = await accounts.AggregateAsync(@"
[
    { $match: { status: 'A' } },
    {
      $redact: {
        $cond: {
          if: { $eq: [ '$level', 5 ] },
          then: '$$PRUNE',
          else: '$$DESCEND'
        }
      }
    }
  ]");
		Console.WriteLine(result.Pretty());

	}

	private static async Task Test3()
	{
		var characters = dbClient?.GetDatabase("test1").GetCollection("characters");

		if (characters == null)
			return;

		//var watchCursor = await characters.WatchAsync("[]", new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup });

		//Console.WriteLine(watchCursor.ToList().Pretty());

		var deletedDocs = await characters.DeleteManyAsync("{}");

		var rr = await characters.InsertManyAsync(@"[
				{ 'char' : 'Londen', 'class' : 'monk', 'lvl' : 4 },
				{ '_id' : 1, 'char' : 'Brisbane', 'class' : 'monk', 'lvl' : 4 },
				{ '_id' : 2, 'char' : 'Eldon', 'class' : 'alchemist', 'lvl' : 3 },
				{ '_id' : 3, 'char' : 'Meldane', 'class' : 'ranger', 'lvl' : 3 }]");

		var result = await characters.BulkWriteAsync(@"
[	
	{
        insertOne: {
            'document': {
                '_id': 4,
                'char': 'Dithras',
                'class': 'barbarian',
                'lvl': 4
            }
        }
    }, {
        insertOne: {
            'document': {
                '_id': 5,
                'char': 'Taeln',
                'class': 'fighter',
                'lvl': 3
            }
        }
    }, {
        updateOne: {
            'filter': {
                'char': 'Eldon'
            },
            'update': {
                $set: {
                    'status': 'Critical Injury'
                }
            }
        }
    }, {
        deleteOne: {
            'filter': {
                'char': 'Brisbane'
            }
        }
    }, {
        replaceOne: {
            'filter': {
                'char': 'Meldane'
            },
            'replacement': {
                'char': 'Tanys',
                'class': 'oracle',
                'lvl': 4
            }
        }
    }
]");



	}

	private static async Task Test4()
	{

		var orders = dbClient?.GetDatabase("test1").GetCollection("orders");

		if (orders == null)
			return;

		//await orders.DeleteManyAsync("{}");

		//			var rr = await orders.InsertManyAsync(@"[
		//   { _id: 0, name: 'Pepperoni', size: 'small', price: 19,
		//     quantity: 10, date: ISODate( '2021-03-13T08:14:30Z' ) },
		//   { _id: 1, name: 'Pepperoni', size: 'medium', price: 20,
		//     quantity: 20, date : ISODate( '2021-03-13T09:13:24Z' ) },
		//   { _id: 2, name: 'Pepperoni', size: 'large', price: 21,
		//     quantity: 30, date : ISODate( '2021-03-17T09:22:12Z' ) },
		//   { _id: 3, name: 'Cheese', size: 'small', price: 12,
		//     quantity: 15, date : ISODate( '2021-03-13T11:21:39.736Z' ) },
		//   { _id: 4, name: 'Cheese', size: 'medium', price: 13,
		//     quantity:50, date : ISODate( '2022-01-12T21:23:13.331Z' ) },
		//   { _id: 5, name: 'Cheese', size: 'large', price: 14,
		//     quantity: 10, date : ISODate( '2022-01-12T05:08:13Z' ) },
		//   { _id: 6, name: 'Vegan', size: 'small', price: 17,
		//     quantity: 10, date : ISODate( '2021-01-13T05:08:13Z' ) },
		//   { _id: 7, name: 'Vegan', size: 'medium', price: 18,
		//     quantity: 10, date : ISODate( '2021-01-13T05:10:13Z' ) }
		//]");



		var list = await orders.AggregateAsync(@"[
   {
      $match: { size: 'medium' }
   },
   {
      $group: { _id: '$name', totalQuantity: { $sum: '$quantity' } }
   }
]");
		Console.WriteLine(list.Pretty());


		var list2 = await orders.AggregateAsync(@"[
   {
      $match:
      {
         'date': { $gte: new ISODate( '2020-01-30' ), $lt: new ISODate( '2022-01-30' ) }
      }
   },
   {
      $group:
      {
         _id: { $dateToString: { format: '%Y-%m-%d', date: '$date' } },
         totalOrderValue: { $sum: { $multiply: [ '$price', '$quantity' ] } },
         averageOrderQuantity: { $avg: '$quantity' }
      }
   },
   {
      $sort: { totalOrderValue: -1 }
   }
 ]");

		Console.WriteLine(list2.Pretty());

	}

}