MongoDB支持对文本内容执行文本搜索操作,其提供了索引text index和查询操作$text来完成文本搜索功能。下面我们通过一个简单的例子来体验一下MongoDB提供的全文检索功能。
1.新建blogs collection,并插入如下的document。
db.blogs.insert({_id:1,title:"MongoDB text search",content:"this is a simple MongoDB text search introduction"})
db.blogs.insert({_id:2,title:"MongoDB text index",content:"this is ae MongoDB text index introduction"})
db.blogs.insert({_id:3,title:"MongoDB text operators",content:"this is ae MongoDB text query introduction"})
2.创建Text Index。
只有拥有text index的collection才支持全文检索;
每个collection只能拥有一个text index;
Text index可以包含任何的string类型、string数组类型的字段;
Text index可以包含多个字段;
执行如下新建text index的语句
db.blogs.ensureIndex({title:"text",content:"text"})