首页 > 数据库技术 > 详细

mongodb 基本操作

时间:2020-12-17 17:23:02      阅读:27      评论:0      收藏:0      [点我收藏+]

插入

//插入 没有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" } } )

 

mongodb 基本操作

原文:https://www.cnblogs.com/shiyuzuxia/p/14150460.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!