Skip to content

Commit 6eeb33a

Browse files
authored
Merge pull request #299 from iceljc/features/create-collection-index
create collection index
2 parents c73a713 + b749670 commit 6eeb33a

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public MongoDbContext(BotSharpDatabaseSettings dbSettings)
1414
_mongoClient = new MongoClient(mongoDbConnectionString);
1515
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString);
1616
_collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp");
17+
//CreateIndex();
1718
}
1819

1920
private string GetDatabaseName(string mongoDbConnectionString)
@@ -28,14 +29,52 @@ private string GetDatabaseName(string mongoDbConnectionString)
2829

2930
private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } }
3031

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+
3160
public IMongoCollection<AgentDocument> Agents
3261
=> Database.GetCollection<AgentDocument>($"{_collectionPrefix}_Agents");
3362

3463
public IMongoCollection<AgentTaskDocument> AgentTasks
35-
=> Database.GetCollection<AgentTaskDocument>($"{_collectionPrefix}_AgentTasks");
64+
{
65+
get
66+
{
67+
return CreateAgentTaskIndex();
68+
}
69+
}
3670

3771
public IMongoCollection<ConversationDocument> Conversations
38-
=> Database.GetCollection<ConversationDocument>($"{_collectionPrefix}_Conversations");
72+
{
73+
get
74+
{
75+
return CreateConversationIndex();
76+
}
77+
}
3978

4079
public IMongoCollection<ConversationDialogDocument> ConversationDialogs
4180
=> Database.GetCollection<ConversationDialogDocument>($"{_collectionPrefix}_ConversationDialogs");

0 commit comments

Comments
 (0)