mongodb 基本操作

插入

//插入 没有stores 会新建stores
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" }
]
}

查询
//建立文本索引 name description
//常对某个字段进行字符串的搜索,为了提高速度可以使用该类型索引。
//一个集合只能有一个文本索引 支持多个字段复合

db.stores.createIndex( { name: "text", description: "text" } )//创建

db.stores.getIndexes() //查询

db.stores.dropIndex("name_text_description_text") //删除 括号里为索引名


//$text:根据文本索引查询
db.stores.find( { $text: { $search: "java,coffee,shop" } } )//查询name,description含有java,coffe,shop 的集合。
db.stores.find( { $text: { $search: "\"coffee shop\"" } } )//加“” 搜索确切的短语
db.stores.find( { $text: { $search: "java shop -coffee" } } )//术语排除 加'-'

//自动计算匹配相关性分数
db.stores.find(
{ $text: { $search: "java coffee shop" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )

 

posted on 2020-12-17 16:41  随想***  阅读(55)  评论(0编辑  收藏  举报