-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
find_fluent.dart
45 lines (34 loc) · 1.38 KB
/
find_fluent.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
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 = 'find';
await db.dropCollection(collectionName);
var collection = db.collection(collectionName);
var ret = await collection.insertMany(<Map<String, dynamic>>[
{'_id': 1, 'name': 'Tom', 'state': 'active', 'rating': 100, 'score': 5},
{'_id': 2, 'name': 'William', 'state': 'busy', 'rating': 80, 'score': 4},
{'_id': 3, 'name': 'Liz', 'state': 'on hold', 'rating': 70, 'score': 8},
{'_id': 4, 'name': 'George', 'state': 'active', 'rating': 95, 'score': 8},
{'_id': 5, 'name': 'Jim', 'state': 'idle', 'rating': 40, 'score': 3},
{'_id': 6, 'name': 'Laureen', 'state': 'busy', 'rating': 87, 'score': 8},
{'_id': 7, 'name': 'John', 'state': 'idle', 'rating': 72, 'score': 7}
]);
if (!ret.isSuccess) {
print('Error detected in record insertion');
}
var res =
await collection.find(where.eq('name', 'Tom').gt('rating', 10)).toList();
print('First document fetched: ${res.first['name']} - ${res.first['state']}');
// Tom - active
await cleanupDatabase();
}