-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
update_many.dart
57 lines (45 loc) · 1.35 KB
/
update_many.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
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-many';
await db.dropCollection(collectionName);
var collection = db.collection(collectionName);
var ret = await collection.insertMany([
{
'_id': 1,
'member': 'abc123',
'status': 'A',
'points': 1,
'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.updateMany(
where, ModifierBuilder().set('status', 'A').inc('points', 1),
writeConcern: WriteConcern(w: 'majority', wtimeout: 5000));
print('Modified documents: ${res.nModified}'); // 2
var findResult = await collection.find().toList();
print('Last record points: ${findResult.last['points']}'); // 60;
await cleanupDatabase();
}