在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" }热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短