【问题标题】:mongo-go-driver (IndexNotFound) text index required for $text query$text 查询需要 mongo-go-driver (IndexNotFound) 文本索引
【发布时间】:2019-08-16 18:08:52
【问题描述】:

我正在使用 mongo-go-driver 并尝试使用文本搜索

我正在创建这样的索引

    opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
    db.Collection("my_collection").Indexes().CreateMany(
        context.Background(),
        []mongo.IndexModel{
            {
                Keys: bsonx.Doc{{"title", bsonx.Int32(-1)}},
            },
            {
                Keys: bsonx.Doc{{"info.tags", bsonx.Int32(-1)}},
            },
        },
        opts,
    )

...在查询时我正在这样做

    collection := db.Collection("my_collection")

    cur, err := collection.Find(context.Background(), bson.M{ "$text": bson.M{ "$search": query }})

我在调用查询时得到这个

 (IndexNotFound) text index required for $text query
  exit status 1

【问题讨论】:

    标签: mongodb go indexing text-search


    【解决方案1】:

    您需要在至少 1 个字段上具有 text index 才能使查询正常工作。 尝试在任何字段上创建文本索引,然后重试

    【讨论】:

    • 请点击对您最有帮助的答案左侧的复选标记来关闭您的问题
    猜你喜欢
    • 2020-09-07
    • 1970-01-01
    • 2020-05-12
    • 2017-10-17
    • 2018-05-18
    • 1970-01-01
    • 2019-07-13
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多