-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathMongoDBExamples.cs
213 lines (191 loc) · 7.17 KB
/
MongoDBExamples.cs
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
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Examples;
using Examples.Models;
using MongoDB.Bson;
using NUnit.Framework;
using Realms;
using Realms.Sync;
namespace Examples
{
public class MongoDBExamples
{
App app;
Realms.Sync.User user;
PartitionSyncConfiguration config;
const string myRealmAppId = Config.AppId;
MongoClient mongoClient;
MongoClient.Database dbPlantInventory;
MongoClient.Collection<Plant> plantsCollection;
[OneTimeSetUp]
public async Task Setup()
{
app = App.Create(myRealmAppId);
user = await app.LogInAsync(Config.EPCreds);
config = new PartitionSyncConfiguration("myPart", user);
// :snippet-start: mongo-setup
// :replace-start: {
// "terms": {
// "dotnet_tests": "inventory"}
// }
mongoClient = user.GetMongoClient("mongodb-atlas");
dbPlantInventory = mongoClient.GetDatabase("dotnet_tests");
plantsCollection = dbPlantInventory.GetCollection<Plant>("plants");
// :replace-end:
// :snippet-end:
await InsertsOne();
await InsertsMany();
return;
}
public async Task InsertsOne()
{
// :snippet-start: mongo-insert-one
var plant = new Plant
{
Name = "Venus Flytrap",
Sunlight = Sunlight.Full.ToString(),
Color = PlantColor.White.ToString(),
Type = PlantType.Perennial.ToString(),
Partition = "Store 42"
};
var insertResult = await plantsCollection.InsertOneAsync(plant);
var newId = insertResult.InsertedId;
// :snippet-end:
}
public async Task InsertsMany()
{
// :snippet-start: mongo-insert-many
var sweetBasil = new Plant
{
Name = "Sweet Basil",
Sunlight = Sunlight.Partial.ToString(),
Color = PlantColor.Green.ToString(),
Type = PlantType.Annual.ToString(),
Partition = "Store 42"
};
var thaiBasil = new Plant
{
Name = "Thai Basil",
Sunlight = Sunlight.Partial.ToString(),
Color = PlantColor.Green.ToString(),
Type = PlantType.Perennial.ToString(),
Partition = "Store 42"
};
var helianthus = new Plant
{
Name = "Helianthus",
Sunlight = Sunlight.Full.ToString(),
Color = PlantColor.Yellow.ToString(),
Type = PlantType.Annual.ToString(),
Partition = "Store 42"
};
var petunia = new Plant
{
Name = "Petunia",
Sunlight = Sunlight.Full.ToString(),
Color = PlantColor.Purple.ToString(),
Type = PlantType.Annual.ToString(),
Partition = "Store 47"
};
var listofPlants = new List<Plant>
{
sweetBasil,
thaiBasil,
helianthus,
petunia
};
var insertResult = await plantsCollection.InsertManyAsync(listofPlants);
var newIds = insertResult.InsertedIds;
// :snippet-end:
}
// [Test]
public async Task ReadsDocuments()
{
// :snippet-start: mongo-find-one
var petunia = await plantsCollection.FindOneAsync(
new { name = "Petunia" },
null);
// :snippet-end:
Assert.AreEqual("Store 47", petunia.Partition);
// :snippet-start: mongo-find-many
var allPerennials = await plantsCollection.FindAsync(
new { type = PlantType.Perennial.ToString() },
new { name = 1 });
// :snippet-end:
Assert.AreEqual(2, allPerennials.Count());
// :snippet-start: mongo-count
var allPlants = await plantsCollection.CountAsync();
// :snippet-end:
Assert.AreEqual(5, allPlants);
}
// [Test]
public async Task UpdatesDocuments()
{
{
// :snippet-start: mongo-update-one
var updateResult = await plantsCollection.UpdateOneAsync(
new { name = "Petunia" },
new BsonDocument("$set", new BsonDocument("sunlight", Sunlight.Partial.ToString()))
);
// :snippet-end:
Assert.AreEqual(1, updateResult.MatchedCount);
Assert.AreEqual(1, updateResult.ModifiedCount);
}
{
// :snippet-start: mongo-update-many
var filter = new { _partition = "Store 47" };
var updateDoc = new BsonDocument("$set",
new BsonDocument("_partition", "Area 51"));
var updateResult = await plantsCollection.UpdateManyAsync(
filter, updateDoc);
// :snippet-end:
Assert.AreEqual(1, updateResult.MatchedCount);
Assert.AreEqual(1, updateResult.ModifiedCount);
}
{
// :snippet-start: mongo-upsert
var filter = new BsonDocument()
.Add("name", "Pothos")
.Add("type", PlantType.Perennial.ToString())
.Add("sunlight", Sunlight.Full.ToString());
var updateResult = await plantsCollection.UpdateOneAsync(
filter,
new BsonDocument("$set", new BsonDocument("_partition", "Store 42")),
upsert: true);
/* The upsert will create the following object:
{
"name": "pothos",
"sunlight": "full",
"type": "perennial",
"_partition": "Store 42"
}
*/
// :snippet-end:
var plant = await plantsCollection.FindOneAsync(filter);
Assert.AreEqual("Store 42", plant.Partition);
Assert.AreEqual(plant.Id, updateResult.UpsertedId);
}
}
[OneTimeTearDown]
public async Task TearDown()
{
// :snippet-start: mongo-delete-one
var filter = new BsonDocument("name", "Thai Basil");
var deleteResult = await plantsCollection.DeleteOneAsync(filter);
// :snippet-end:
// :snippet-start: mongo-delete-many
// :replace-start: {
// "terms": {
// "filter2": "filter",
// "deleteResult2": "deleteResult" }
// }
var filter2 = new BsonDocument("type", PlantType.Annual);
var deleteResult2 = await plantsCollection.DeleteManyAsync(filter);
// :replace-end:
// :snippet-end:
await plantsCollection.DeleteManyAsync();
return;
}
}
}