使用表达式为数组中的字段创建文本索引

han*_*aad 5 c# mongodb mongodb-csharp-2.0 mongodb-.net-driver

我想为数组中的多个字段和元素字段创建文本索引.目前我将数组元素的路径定义为字符串,这是有效的.有没有办法像我可以对这样的简单字段一样使用表达式:

var textIndex = Builders<Project>.IndexKeys
    .Text(p => p.Name)
    .Text(p => p.Description)
    // This and any other expression I tried does not work
    //.Text(p => p.System.Elements.SelectMany(e => e.Name))
    // But this works fine:
    .Text("system.elements.name");

await collection.Indexes.CreateOneAsync(textIndex);
Run Code Online (Sandbox Code Playgroud)

我正在使用mongodb 3.2和MongoDB.Driver 2.2.2

小智 0

我自己来这里寻找解决方案,你的想法似乎可行。我仍然只是对我的代码进行原型设计,所以它可能会更好,但对我有用的是:

private static IMongoDatabase db = dataConnector.ConnectDatabase();

private void CreateIndex(string collection)
{
    var CompoundTextIndex = new CreateIndexModel<MyDataModel>
        (Builders<MyDataModel>.IndexKeys
        .Text(x=> x.name)
        .Text(x => x.parentCompany));

    db.GetCollection<MyDataModel>
        (collection).Indexes.CreateOne(CompoundTextIndex);
}
Run Code Online (Sandbox Code Playgroud)