mongodb使用笔记

Update

db.collection.update(
   ,,{
     upsert: ,multi: ,writeConcern: 
   }
)

1.部分更新

db.teacher_collection.update({name:"cobble"},{age:55})

更新时,新的对象会覆盖原有的对象,上面的更新会把cobble对象的其他字段都删除,最后只有age字段;如果需要部分更新,使用 $set

db.teacher_collection.update({name:"cobble"},{$set:{age:55}})

删除数据

db.collection.remove(
   ,{
     justOne: ,writeConcern: ,collation: 
   }
)

remove必须带有参数,如果想删除所有数据就drop掉这个表。

创建全文索引

mongodb中一个集合只能包含一个全文索引

db.collection.createIndex({key:"text"});  //在key字段上建立全文索引
db.collection.createIndex({key_1:"text",key_2:"text"}); //在key_1和key_2字段上建立全文索引
db.collection.createIndex({"$**":"text"});   //在集合中所有字段上建立全文索引
db.collection.createIndex( { a: 1,"$**": "text" } ) //与所有文本索引一样,通配符文本索引可以是复合索引的一部分

使用全文索引查询

db.collection.find({$text:{$search:"coffee"}});    //全文检索包含coffe的记录
db.collection.find({$text:{$search:"coffee cup"}});  //全文检索包含coffee或cup的记录,空格表示或
db.collection.find({$text:{$search:"coffee cup -cat"}});   //全文检索包含coffee或cup,但包含cat的记录
db.collection.find({$text:{$search:"\"coffee\" \"cup\""}}); //全文检索包含coffee和cup的记录,双引号表示且

相似度查询

//查询出相似度,并根据相似度进行排序
db.collection.find({$text:{$search:"coffee cup"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}});

全文索引的限制

  • 每次查询只能使用一个$text查询
  • $text查询不能出现在$nor查询中
  • 查询中如果使用了$text,hint则不再起作用
  • mongodb全文检索还不支持中文

索引的稀疏性

db.collection.createIndex({key:1},{sparse:true})

查找只存在指定字段的数据

`db.collection.find({fieldname:{$exists:true}})

位置索引

mongostat工具

mongostat -h ip:port

profile在项目早期使用,后期数据较大时不建议用,影响性能

在配置文件中配置verbose来这只日志的详细度,verbose的值是一个到五个v,v越多日志记录越详细

权限认证

配置文件中auth = true

创建用户

相关文章

文章浏览阅读552次。com.mongodb.MongoQueryException: Quer...
文章浏览阅读635次,点赞9次,收藏8次。MongoDB 是一种 NoSQ...
文章浏览阅读2.1k次。和。_mongodb 日期类型
文章浏览阅读1.7k次。Scalestack等客户期待使用MongoDB Atla...
文章浏览阅读970次。SpringBoot整合中间件mongodb、ES_sprin...
文章浏览阅读673次。MongoDB 简介_尚医通sql