-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
update_one.dart
58 lines (46 loc) · 1.42 KB
/
update_one.dart
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
import 'package:mongo_dart/mongo_dart.dart';
const dbName = 'mongo-dart-example';
const dbAddress = '127.0.0.1';
const defaultUri = 'mongodb://$dbAddress:27017/$dbName';
void main() async {
var db = Db(defaultUri);
await db.open();
Future cleanupDatabase() async {
await db.close();
}
if (!db.masterConnection.serverCapabilities.supportsOpMsg) {
return;
}
var collectionName = 'update-one';
await db.dropCollection(collectionName);
var collection = db.collection(collectionName);
var ret = await collection.insertMany([
{
'_id': 1,
'member': 'abc123',
'status': 'Pending',
'points': 0,
'misc1': 'note to self: confirm status',
'misc2': 'Need to activate'
},
{
'_id': 2,
'member': 'xyz123',
'status': 'D',
'points': 59,
'misc1': 'reminder: ping me at 100pts',
'misc2': 'Some random comment'
},
]);
if (!ret.isSuccess) {
print('Error detected in record insertion');
}
var res = await collection.updateOne(where.eq('member', 'abc123'),
ModifierBuilder().set('status', 'A').inc('points', 1),
writeConcern: WriteConcern(w: 'majority', wtimeout: 5000));
print('Modified documents: ${res.nModified}'); // 1
var findResult = await collection.find(where.eq('member', 'abc123')).toList();
print('Modified element status: '
'"${findResult.first['status']}"'); // 'A';
await cleanupDatabase();
}