@@ -14,6 +14,7 @@ public MongoDbContext(BotSharpDatabaseSettings dbSettings)
14
14
_mongoClient = new MongoClient ( mongoDbConnectionString ) ;
15
15
_mongoDbDatabaseName = GetDatabaseName ( mongoDbConnectionString ) ;
16
16
_collectionPrefix = dbSettings . TablePrefix . IfNullOrEmptyAs ( "BotSharp" ) ;
17
+ //CreateIndex();
17
18
}
18
19
19
20
private string GetDatabaseName ( string mongoDbConnectionString )
@@ -28,14 +29,52 @@ private string GetDatabaseName(string mongoDbConnectionString)
28
29
29
30
private IMongoDatabase Database { get { return _mongoClient . GetDatabase ( _mongoDbDatabaseName ) ; } }
30
31
32
+ private IMongoCollection < ConversationDocument > CreateConversationIndex ( )
33
+ {
34
+ var collection = Database . GetCollection < ConversationDocument > ( $ "{ _collectionPrefix } _Conversations") ;
35
+ var indexes = collection . Indexes . List ( ) . ToList ( ) ;
36
+ var createTimeIndex = indexes . FirstOrDefault ( x => x . GetElement ( "name" ) . ToString ( ) . StartsWith ( "CreatedTime" ) ) ;
37
+ if ( createTimeIndex == null )
38
+ {
39
+ var indexDef = Builders < ConversationDocument > . IndexKeys . Descending ( x => x . CreatedTime ) ;
40
+ collection . Indexes . CreateOne ( new CreateIndexModel < ConversationDocument > ( indexDef ) ) ;
41
+ }
42
+
43
+ return collection ;
44
+ }
45
+
46
+ private IMongoCollection < AgentTaskDocument > CreateAgentTaskIndex ( )
47
+ {
48
+ var collection = Database . GetCollection < AgentTaskDocument > ( $ "{ _collectionPrefix } _AgentTasks") ;
49
+ var indexes = collection . Indexes . List ( ) . ToList ( ) ;
50
+ var createTimeIndex = indexes . FirstOrDefault ( x => x . GetElement ( "name" ) . ToString ( ) . StartsWith ( "CreatedTime" ) ) ;
51
+ if ( createTimeIndex == null )
52
+ {
53
+ var indexDef = Builders < AgentTaskDocument > . IndexKeys . Descending ( x => x . CreatedTime ) ;
54
+ collection . Indexes . CreateOne ( new CreateIndexModel < AgentTaskDocument > ( indexDef ) ) ;
55
+ }
56
+
57
+ return collection ;
58
+ }
59
+
31
60
public IMongoCollection < AgentDocument > Agents
32
61
=> Database . GetCollection < AgentDocument > ( $ "{ _collectionPrefix } _Agents") ;
33
62
34
63
public IMongoCollection < AgentTaskDocument > AgentTasks
35
- => Database . GetCollection < AgentTaskDocument > ( $ "{ _collectionPrefix } _AgentTasks") ;
64
+ {
65
+ get
66
+ {
67
+ return CreateAgentTaskIndex ( ) ;
68
+ }
69
+ }
36
70
37
71
public IMongoCollection < ConversationDocument > Conversations
38
- => Database . GetCollection < ConversationDocument > ( $ "{ _collectionPrefix } _Conversations") ;
72
+ {
73
+ get
74
+ {
75
+ return CreateConversationIndex ( ) ;
76
+ }
77
+ }
39
78
40
79
public IMongoCollection < ConversationDialogDocument > ConversationDialogs
41
80
=> Database . GetCollection < ConversationDialogDocument > ( $ "{ _collectionPrefix } _ConversationDialogs") ;
0 commit comments