-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
find_one.dart
98 lines (87 loc) · 2.03 KB
/
find_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
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
import 'package:mongo_dart/mongo_dart.dart';
import 'package:uuid/uuid.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-one';
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,
'userId': UuidValue.fromString('025456da-9e39-4e7c-b1f7-0f5a5e1cb212')
},
{
'_id': 2,
'name': 'William',
'state': 'busy',
'rating': 80,
'score': 4,
'userId': Uuid().v4obj()
},
{
'_id': 3,
'name': 'Liz',
'state': 'on hold',
'rating': 70,
'score': 8,
'userId': Uuid().v4obj()
},
{
'_id': 4,
'name': 'George',
'state': 'active',
'rating': 95,
'score': 8,
'userId': Uuid().v4obj()
},
{
'_id': 5,
'name': 'Jim',
'state': 'idle',
'rating': 40,
'score': 3,
'userId': Uuid().v4obj()
},
{
'_id': 6,
'name': 'Laureen',
'state': 'busy',
'rating': 87,
'score': 8,
'userId': Uuid().v4obj()
},
{
'_id': 7,
'name': 'John',
'state': 'idle',
'rating': 72,
'score': 7,
'userId': Uuid().v4obj()
}
]);
if (!ret.isSuccess) {
print('Error detected in record insertion');
}
var res = await collection.findOne(where.eq('name', 'Tom').gt('rating', 10));
if (res == null) {
print('No document found');
} else {
print('Document fetched: '
'${res['name']} - ${res['state']} - ${res['userId']}');
} // Tom - active - 025456da-9e39-4e7c-b1f7-0f5a5e1cb212
await cleanupDatabase();
}