在 MongoDB 中为文本搜索创建索引
让我们创建一个带文档的集合 -
> db.demo331.insertOne({"Words":"This is a MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e521c35f8647eb59e562089") } > db.demo331.insertOne({"Words":"THIS is a MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e521c36f8647eb59e56208a") }
在集合中使用 find() 方法显示所有文档 -
> db.demo331.find();
这将产生以下输出 -
{ "_id" : ObjectId("5e521c35f8647eb59e562089"), "Words" : "This is a MySQL" } { "_id" : ObjectId("5e521c36f8647eb59e56208a"), "Words" : "THIS is a MongoDB" }
以下是为文本搜索创建索引的查询 -
> db.demo331.createIndex( {Words: "text" } ); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo331.find({$text:{$search:"MySQL"}});
这将产生以下输出 -
{ "_id" : ObjectId("5e521c35f8647eb59e562089"), "Words" : "This is a MySQL" }
广告