-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
delete_one.dart
42 lines (30 loc) · 1.01 KB
/
delete_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
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 = 'delete-one';
await db.dropCollection(collectionName);
var collection = db.collection(collectionName);
var ret = await collection.insertMany([
{'_id': 3, 'name': 'John', 'age': 32},
{'_id': 4, 'name': 'Mira', 'age': 27},
{'_id': 7, 'name': 'Luis', 'age': 42},
]);
if (!ret.isSuccess) {
print('Error detected in record insertion');
}
var res = await collection.deleteOne(<String, Object>{});
print('Removed documents: ${res.nRemoved}');
var findResult = await collection.find().toList();
print('First record name: ${findResult.first['name']}'); // 'Mira';
await cleanupDatabase();
}