Skip to content

Commit 3265622

Browse files
authored
Merge pull request #4 from oslabs-beta/emilia/query2
updated query.ts
2 parents ae73d0f + f8976ae commit 3265622

File tree

1 file changed

+100
-3
lines changed

1 file changed

+100
-3
lines changed

lib/query.ts

+100-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@
33
import { Collection } from '../deps.ts';
44
import { Connection } from './connections.ts';
55

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+
618
class Query {
719
public collectionName: string;
820
// Refactor how connection is brought in
921
public connection: Connection;
10-
22+
1123
constructor(collectionName: string) {
1224
this.collectionName = collectionName;
1325
this.connection = new Connection(
1426
'mongodb+srv://wgreco13:g3HUuathwbVEisEj@cluster0.adcc3.mongodb.net/dangoDB?authMechanism=SCRAM-SHA-1'
1527
);
1628
}
17-
29+
/*Returns one document that satisfies the specified query criteria on the collection or view. */
1830
public async findOne(queryObject: Record<string, string>) {
1931
try {
2032
const db = await this.connection.connect();
@@ -24,13 +36,98 @@ class Query {
2436
console.log(data);
2537

2638
await this.connection.disconnect();
39+
2740
} catch (error) {
2841
throw new Error(`Error in findOne function. ${error}`);
2942
}
3043
}
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+
31119
}
32120

121+
33122
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 }}}])
35129

36130
export { Query };
131+
132+
133+

0 commit comments

Comments
 (0)