【问题标题】:Create a text index for fields in array using an expression使用表达式为数组中的字段创建文本索引
【发布时间】:2016-02-01 15:45:07
【问题描述】:

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

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);

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

【问题讨论】:

标签: c# mongodb mongodb-.net-driver mongodb-csharp-2.0


【解决方案1】:

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

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);
}

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 2019-12-20
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多