-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.ts
43 lines (37 loc) · 1.17 KB
/
mongodb.ts
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
import { MongoClient, Collection, Document } from 'mongodb';
import * as credentials from './credentials.json';
export class MongoDB {
private client: MongoClient;
private sentences: Collection<Document> | undefined;
constructor() {
this.client = new MongoClient(credentials.mongoUrl);
}
async Clear() {
await this.client.connect();
this.sentences = this.client.db('main').collection('sentences');
this.sentences!.drop();
await this.client.db('main').createCollection(
"sentences",
{
timeseries: {
timeField: "timestamp",
metaField: "sessionId",
granularity: "minutes"
},
}
);
this.sentences = this.client.db('main').collection('sentences');
// this.sentences.createIndex({ sessionId: 1 }); // Ascending index
}
async Write(time: Date, sessionId: string, sentence: string) {
const doc = { timestamp: time, sessionId: sessionId, sentence: sentence };
await this.sentences!.insertOne(doc);
}
async Read() {
const query = { sessionId: { $eq: 6478 } };
await this.sentences!.find(query).toArray();
}
async CleanUp() {
await this.client.close();
}
}