mongodb拷贝

1.mongodb特性
1)mongo是一个面向文档的数据库,它集合了nosql和sql数据库两方面的特性。
2)所有实体都是在首次使用时创建。
3)没有严格的事务特性,但是它保证任何一次数据变更都是原子性的。
4)也没有固定的数据模型
5)mongo以javascript作为命令行执行引擎,所以利用shell进行复杂的计算和查询时会相当的慢。
6)mongo本身支持集群和数据分片
7)mongo是c++实现的,支持windows mac linux等主流操作系统
8)性能优越,速度快
2.mongo常用操作
1.增删操作
db.user.insert({name:'aaaa',age:30});
db.user.save({name:'aaaa',age:30});
db.collection.insertOne({});(3.2新特性)
db.collection.deleteOne(<filter>,{});(3.2新特性)
db.collection.remove({name:'aaa'});
db.collection.remove();(删除全部)

2.更新操作
db.users.update ({ " name" : "joe" }, joe );
db.users.update ({ " name" : "joe" }, joe, true );------upsert模式
db.users.update ({ " name" : "joe" }, joe, true ,true);------MULTI模式
update是对文档替换,而不是局部修改默认情况update更新匹配的第一条文档,multi模式更新所有匹配的
3.查询操作
-- 普通查询
db.user.find();
db.user.find({name:'aaa'});
db.user.findOne({name:'aaa'});
-- 模糊查询
db.UserInfo.find({userName :/A/}) (名称%A%)
db.UserInfo.find({userName :/^A/}) (名称A%)
4.操作符
1.$lt, $lte,$gt, $gte(<, <=, >, >= )
2.$all 数组中的元素是否完全匹配 db.things.find( { a: { $all: [ 2, 3 ] } } );
3.$exists 可选:true,false db.things.find( { a : { $exists : true } } );
4.$mod 取模:a % 10 == 1 db.things.find( { a : { $mod : [ 10 , 1 ] } } );
5.$ne 取反:即not equals db.things.find( { x : { $ne : 3 } } );
6.$in 类似于SQL的IN操作 db.things.find({j:{$in: [2,4,6]}});
7.$nin $in的反操作,即SQL的 NOT IN db.things.find({j:{$nin: [2,4,6]}});
8.$nor $or的反操作,即不匹配(a或b) db.things.find( { name : "bob", $nor : [ { a : 1 },{ b : 2 }]})
9.$or Or子句,注意$or不能嵌套使用 db.things.find( { name : "bob" , $or : [ { a : 1 },{ b : 2 }]})
10.$size 匹配数组长度 db.things.find( { a : { $size: 1 } } );
11.$type 匹配子键的数据类型,详情请看 db.things.find( { a : { $type : 2 } } );
5.数组查询
$size 用来匹配数组长度(即最大下标)
// 返回comments包含5个元素的文档
db.posts.find({}, {comments:{‘$size': 5}});
// 使用冗余字段来实现
db.posts.find({}, {‘commentCount': { ‘$gt': 5 }});
$slice 操作符类似于子键筛选,只不过它筛选的是数组中的项
// 仅返回数组中的前5项
db.posts.find({}, {comments:{‘$slice': 5}});
// 仅返回数组中的最后5项
db.posts.find({}, {comments:{‘$slice': -5}});
// 跳过数组中的前20项,返回接下来的10项
db.posts.find({}, {comments:{‘$slice': [20, 10]}});
// 跳过数组中的最后20项,返回接下来的10项
db.posts.find({}, {comments:{‘$slice': [-20, 10]}});
MongoDB 允许在查询中指定数组的下标,以实现更加精确的匹配
// 返回comments中第1项的by子键为Abe的所有文档
db.posts.find( { "comments.0.by" : "Abe" } );
3.索引的使用
1.创建索引
db.things.ensureIndex ({'j': 1});
创建子文档 索引
db.things.ensureIndex ({'user.Name' : - 1});
创建 复合 索引
db.things.ensureIndex ({
'j' : 1 , // 升序
'x' : - 1 // 降序
});
如果 您的 find 操作只用到了一个键,那么索引方向是无关紧要的
当创建复合索引的时候,一定要谨慎斟酌每个键的排序方向
2.修改索引
修改索引,只需要重新 运行索引 命令即可
如果索引已经存在则会 重建, 不存在的索引会被 添加
db . things . ensureIndex ({
--- 原来的索引会 重建
'user.Name ' : - 1 ,
--- 新增一个升序 索引
'user.Name ' : 1 ,
--- 为 Age 新建降序 索引
'user.Age ' : - 1
},
打开后台执行
{ ‘background' : true}
);
重建索引
db. things .reIndex();
3.删除索引
删除集合中的所有 索引
db . things . dropIndexes ();
删除指定键的索引
db.things.dropIndex ({
x : 1 ,
y : - 1
});
使用 command 删除指定键的 索引
db.runCommand ({
dropIndexes : 'foo ' ,
index : { y : 1 }
});
使用 command 删除所有 索引
db . runCommand ({dropIndexes : 'foo ' ,index : '*‘})
如果是删除集合中所有的文档(remove)则不会影响索引,当有新文档插入时,索引就会重建。
4.唯一索引
创建唯一索引,同时这也是一个符合唯一索引
db.things.ensureIndex (
{
'firstName ' : 1 ,
'lastName ' : 1
}, {
指定为唯一索引
'unique ' : true ,
删除重复 记录
'dropDups ' : true
});
5、强制使用索引
强制使用索引 a 和 b
db.collection.find ({
'a ' : 4 ,
'b ' : 5 ,
'c ' : 6
}). hint ({
'a ' : 1 ,
'b ' : 1
});
强制不使用任何 索引
db.collection.find ().hint ({
'$ natural' : 1
});
索引总结:
索引可以加速查询;
单个索引无需在意其索引方向;
多键索引需要慎重考虑每个索引的方向;
做海量数据更新时应当先卸载所有索引,待数据更新完成后再重建索引;
不要试图为每个键都创建索引,应考虑实际需要,并不是索引越多越好;
唯一索引可以用来消除重复记录;
地理空间索引是没有单位的,其内部实现是基本的勾股定理算法

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 学习《MongoDB 权威指南·第2版》的笔记,结合 MongoDB 官方最新文档(v3.6),简单记录一些概念、...
    小鱼爱小虾阅读 6,213评论 0 5
  • 目录 查询操作 集合查询方法 find() 查询内嵌文档 查询操作符(内含 数组查询) "$gt" 、"$gte"...
    彩虹之梦阅读 1,128评论 0 1
  • NoSql数据库优缺点 在优势方面主要体现在下面几点: 简单的扩展 快速的读写 低廉的成本 灵活的数据模型 在不足...
    dreamer_lk阅读 2,877评论 0 6
  • MongoDB常用操作 一、查询 find方法 查询所有的结果: select * from users;===d...
    止风者阅读 651评论 1 3
  • 成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作。输入help可以看到基本...
    精气神贯通阅读 509评论 0 0

友情链接更多精彩内容