-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathdelete_one_collation.dart
63 lines (47 loc) · 1.84 KB
/
delete_one_collation.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
59
60
61
62
63
import 'package:mongo_dart/mongo_dart.dart'
show CollationOptions, Db, FindOptions, where;
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 = 'delete-one-collation';
await db.dropCollection(collectionName);
var collection = db.collection(collectionName);
var ret = await collection.insertMany(<Map<String, dynamic>>[
{'_id': 1, 'category': 'café', 'status': 'A'},
{'_id': 2, 'category': 'cafE', 'status': 'a'},
{'_id': 3, 'category': 'cafe', 'status': 'a'},
]);
if (!ret.isSuccess) {
print('Error detected in record insertion');
}
var findResult = await collection
.modernFind(selector: where.eq('category', 'cafe').eq('status', 'a'))
.toList();
print('First record category without collation before deletion: '
'${findResult.first['category']}'); // 'cafe';
findResult = await collection
.modernFind(
selector: where.eq('category', 'cafe').eq('status', 'a'),
findOptions:
FindOptions(collation: CollationOptions('fr', strength: 1)))
.toList();
print('First record category with collation before deletion: '
'${findResult.first['category']}'); // 'café';
var res = await collection.deleteOne(
<String, Object>{'category': 'cafe', 'status': 'a'},
collation: CollationOptions('fr', strength: 1));
print('Removed documents: ${res.nRemoved}');
findResult = await collection.find().toList();
print('First record category after deletion with collation: '
'${findResult.first['category']}'); // 'cafE';
await cleanupDatabase();
}