-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
ordered_collection_helper.dart
117 lines (110 loc) · 4.62 KB
/
ordered_collection_helper.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
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
import 'package:mongo_dart/mongo_dart.dart';
void main() async {
final db = Db('mongodb://127.0.0.1/testdb');
await db.open();
var collection = db.collection('orderedBulkHelper');
// clean data if the example is run more than once.
await collection.drop();
/// Bulk write returns an instance of `BulkWriteResult` class
/// that is a convenient way of reading the result data.
/// If you prefer the server response, a serverResponses list of
/// document is available in the `BulkWriteResult` object.
var ret = await collection.bulkWrite([
{
/// Insert many is specific to the mongo_dart driver.
/// The mongo shell does not have this method
/// It is similar to the `insertMany` method, with the difference
/// that here we have no limit of document numbers, while the `insertMany`
/// is limited depending on the MongoDb version (recent releases
/// set this limit to 100,000 documents).
///
/// You can use the convenient constant (here bulkInsertMany)
/// or a simple string ('insertMany').
bulkInsertMany: {
bulkDocuments: [
{'cust_num': 99999, 'item': 'abc123', 'status': 'A'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'D'},
{'cust_num': 12911, 'item': 'book1', 'status': 'A'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'D'},
{'cust_num': 81237, 'item': 'sample', 'status': 'A'},
{'cust_num': 99999, 'item': 'book1', 'status': 'D'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'A'},
{'cust_num': 12911, 'item': 'sample', 'status': 'A'},
{'cust_num': 12345, 'item': 'book1', 'status': 'A'},
{'cust_num': 81237, 'item': 'abc123', 'status': 'A'},
{'cust_num': 12911, 'item': 'sample', 'status': 'D'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'D'},
{'cust_num': 12911, 'item': 'book1', 'status': 'R'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'A'},
{'cust_num': 99999, 'item': 'tst24', 'status': 'S'},
{'cust_num': 99999, 'item': 'abc123', 'status': 'D'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'A'},
{'cust_num': 12911, 'item': 'sample', 'status': 'D'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'A'},
{'cust_num': 81237, 'item': 'book1', 'status': 'A'},
{'cust_num': 99999, 'item': 'abc123', 'status': 'D'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'R'},
{'cust_num': 12911, 'item': 'book1', 'status': 'A'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'D'},
{'cust_num': 81237, 'item': 'tst24', 'status': 'A'},
{'cust_num': 12911, 'item': 'book1', 'status': 'S'},
{'cust_num': 12345, 'item': 'sample', 'status': 'D'},
{'cust_num': 12911, 'item': 'tst24', 'status': 'A'},
{'cust_num': 12345, 'item': 'book1', 'status': 'D'},
{'cust_num': 99999, 'item': 'tst24', 'status': 'A'},
{'cust_num': 81237, 'item': 'abc123', 'status': 'D'},
{'cust_num': 12911, 'item': 'book1', 'status': 'A'},
{'cust_num': 12345, 'item': 'tst24', 'status': 'D'},
{'cust_num': 12911, 'item': 'book1', 'status': 'A'},
{'cust_num': 12345, 'item': 'sample', 'status': 'R'},
]
}
},
{
/// here also you can use the convenient constants or the string
/// like in Mongo shell
bulkUpdateMany: {
bulkFilter: {'status': 'D'},
bulkUpdate: {
r'$set': {'status': 'd'}
}
}
},
{
bulkUpdateOne: {
bulkFilter: {'cust_num': 99999, 'item': 'abc123', 'status': 'A'},
bulkUpdate: {
r'$inc': {'ordered': 1}
}
}
},
{
bulkReplaceOne: {
bulkFilter: {'cust_num': 12345, 'item': 'tst24', 'status': 'D'},
bulkReplacement: {
'cust_num': 12345,
'item': 'tst24',
'status': 'Replaced'
},
bulkUpsert: true
}
}
/// If ordered, operations will be executed in the exact order in which
/// you have declared them, and an error will stop the execution
/// avoiding to process the remaining ones.
/// Note
/// The default is true, so the below parameter is our case is not
/// mandatory
], ordered: true);
print(ret.ok); // 1.0
print(ret.operationSucceeded); // true
print(ret.hasWriteErrors); // false
print(ret.hasWriteConcernError); // false
print(ret.nInserted); // 35
print(ret.nUpserted); // 1
print(ret.nModified); // 14
print(ret.nMatched); // 15
print(ret.nRemoved); // 0
print(ret.isSuccess); // true
await db.close();
}