//插入测试数据 有列name和description
> db.stores.insert(
... [
... { _id: 1, name: "Java Hut", description: "Coffee and cakes" },
... { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
... { _id: 3, name: "Coffee Shop", description: "Just coffee" },
... { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
... { _id: 5, name: "Java Shopping", description: "Indonesian goods" }
... ]
... )
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 5,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
//在stores上建立所以 包含name列和description都是文本
> db.stores.createIndex( { name: "text", description: "text" } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
//执行全文检索 会将关键字分词 然后匹配结果还可以 由于数据量小 速度就测不出来了
> db.stores.find( { $text: { $search: "java coffee shop" } } )
{ "_id" : 3, "name" : "Coffee Shop", "description" : "Just coffee" }
{ "_id" : 1, "name" : "Java Hut", "description" : "Coffee and cakes" }
{ "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods" }