-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathinsert_one.dart
More file actions
42 lines (31 loc) · 862 Bytes
/
insert_one.dart
File metadata and controls
42 lines (31 loc) · 862 Bytes
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 = 'insert-one';
await db.dropCollection(collectionName);
var collection = db.collection(collectionName);
var ret = await collection.insertOne(<String, dynamic>{
'_id': 1,
'name': 'Tom',
'state': 'active',
'rating': 100,
'score': 5
});
if (!ret.isSuccess) {
print('Error detected in record insertion');
}
var res = await collection.findOne();
print('Fetched ${res?['name']}');
// Tom
await cleanupDatabase();
}