3
3
import { Collection } from '../deps.ts' ;
4
4
import { Connection } from './connections.ts' ;
5
5
6
+ interface MatchInterface {
7
+ $match : { [ unknownKeyName : string ] : string } ;
8
+ }
9
+
10
+ interface GroupInterface {
11
+ $group : {
12
+ [ unknownKeyName : string ] : string | { $sum : number } ;
13
+ }
14
+
15
+ }
16
+
17
+
6
18
class Query {
7
19
public collectionName : string ;
8
20
// Refactor how connection is brought in
9
21
public connection : Connection ;
10
-
22
+
11
23
constructor ( collectionName : string ) {
12
24
this . collectionName = collectionName ;
13
25
this . connection = new Connection (
14
26
'mongodb+srv://wgreco13:g3HUuathwbVEisEj@cluster0.adcc3.mongodb.net/dangoDB?authMechanism=SCRAM-SHA-1'
15
27
) ;
16
28
}
17
-
29
+ /*Returns one document that satisfies the specified query criteria on the collection or view. */
18
30
public async findOne ( queryObject : Record < string , string > ) {
19
31
try {
20
32
const db = await this . connection . connect ( ) ;
@@ -24,13 +36,98 @@ class Query {
24
36
console . log ( data ) ;
25
37
26
38
await this . connection . disconnect ( ) ;
39
+
27
40
} catch ( error ) {
28
41
throw new Error ( `Error in findOne function. ${ error } ` ) ;
29
42
}
30
43
}
44
+ /*Selects documents in a collection or view and returns a cursor to the selected documents. */
45
+ public async find ( allQueryObjects ?: object ) {
46
+ try {
47
+ const db = await this . connection . connect ( ) ;
48
+
49
+ const collection = db . collection ( this . collectionName ) ;
50
+ const data = await collection . find ( allQueryObjects ) ;
51
+ const dataRes = await data . toArray ( )
52
+
53
+ console . log ( dataRes ) ;
54
+
55
+ await this . connection . disconnect ( ) ;
56
+
57
+ } catch ( error ) {
58
+ throw new Error ( `Error in find function. ${ error } ` ) ;
59
+ }
60
+ }
61
+ /* Returns the count of documents that match the query for a collection or view. */
62
+ public async countDocuments ( queryObject : Record < string , string > ) {
63
+ try {
64
+ const db = await this . connection . connect ( ) ;
65
+
66
+ const collection = db . collection ( this . collectionName ) ;
67
+ const data = await collection . countDocuments ( queryObject ) ;
68
+
69
+
70
+ console . log ( data ) ;
71
+
72
+ await this . connection . disconnect ( ) ;
73
+
74
+ } catch ( error ) {
75
+ throw new Error ( `Error in countDocuments function. ${ error } ` ) ;
76
+ }
77
+ }
78
+ /*estimatedDocumentCount() = Returns the count of all documents in a collection or view. The method wraps the count command. */
79
+ public async estimatedDocumentCount ( ) {
80
+ try {
81
+ const db = await this . connection . connect ( ) ;
82
+
83
+ const collection = db . collection ( this . collectionName ) ;
84
+ const data = await collection . estimatedDocumentCount ( ) ;
85
+
86
+ console . log ( data ) ;
87
+
88
+ await this . connection . disconnect ( ) ;
89
+
90
+ } catch ( error ) {
91
+ throw new Error ( `Error in estimatedDocumentCount function. ${ error } ` ) ;
92
+ }
93
+ }
94
+ /*Aggregation operations process multiple documents and return computed results. You can use aggregation operations to:
95
+ Group values from multiple documents together.
96
+ Perform operations on the grouped data to return a single result.
97
+ Analyze data changes over time. */
98
+
99
+
100
+
101
+ // public async aggregate(arg1: [{$match:{[unknownKeyName:string]: string}}]) {
102
+ public async aggregate ( arg1 : MatchInterface [ ] , arg2 : GroupInterface [ ] ) {
103
+ const db = await this . connection . connect ( ) ;
104
+
105
+ const collection = db . collection ( this . collectionName ) ;
106
+ const data = await collection . aggregate ( arg1 , arg2 ) ;
107
+ const dataRes = await data . toArray ( )
108
+
109
+ console . log ( dataRes ) ;
110
+
111
+ await this . connection . disconnect ( ) ;
112
+
113
+ } catch ( error ) {
114
+ throw new Error ( `Error in aggregate function. ${ error } ` ) ;
115
+ }
116
+
117
+
118
+
31
119
}
32
120
121
+
33
122
const query = new Query ( 'new' ) ;
34
- query . findOne ( { username : 'test' } ) ;
123
+
124
+ // query.findOne({ username: 'test' });
125
+ // query.find()
126
+ // query.countDocuments({ username: 'test' });
127
+ // query.estimatedDocumentCount();
128
+ query . aggregate ( [ { $match : { username : 'test' } } , { $group : { _id : '$username' , total : { $sum : 1 } } } ] )
35
129
36
130
export { Query } ;
131
+
132
+
133
+
0 commit comments