广东建设官方网站/今日微博热搜榜前十名
每条记录都会有一个_id作为唯一识别号,当插入数据时会自动为_id创建索引
-- 查看所有数据库
show dbs;
-- 切换数据库
use imooc;(没有则自动创建)
-- 删除数据库
db.dropDatabase();
--插入数据
db.imooc_collection.insert({x:1});
-- 查询创建的集合(表)
show collections;
--查询数据
db.imooc_collection.find();
--查询一条数据
db.imooc_collection.findOne();
--带条件查询数据
db.imooc_collection.find({x:1});
--批量插入数据
for(i == 3; i < 100; i++)db.imooc_collection_insert({x:1});
-- 查询总条数
db.imooc_collection.find().count();
--从第四条数据开始,显示两条,以x排序
db.imooc_collection.find().skip(3).limit(2).sort({x:1});
--数据更新
db.imooc_collection.update({x:1}, {x:999});
-- 查找出z=100的数据,把y更新为99
db.imooc_collection.update({z:100}, {$set:{y:99}});
--如果更新的数据不存在则自动插入
db.imooc_collection.update({y:100}, {y:999}, true);
-- 批量更新c=1的数据为c=2,第一个false表示不存在不自动插入,第二个true表示全部更新,否则mongodb只会更新第一条数据(防止误操作)
db.imooc_collection.update({c:1}, {$set{c:2}}, false, true);
-- 删除所有c=2的数据
db.imooc_collection.remove({c:2});
--删除整个集合(表)
db.imooc_collection.drop();
MongoDB支持的索引类型:
1._id索引
2.单键索引
3.多键索引
4.复合索引
5.过期索引
6.全文索引
7.地理位置索引
--查询所有表(类似show collections)
show tables;
--查看mongodb中的索引
db.imooc_collection.getIndexes();
-- 创建索引,对imooc_collection表x创建索引,1为正序,-1为逆序
db.imooc_collection.ensureIndex({x:1});
-- 当插入此条数据时,mongodb为x创建了多键索引
db.imooc2.insert({x:[1,2,3,4,5]});
--创建复合索引
db.imooc2.ensureIndex({x:1}, {y:1});
过期索引:一段时间后会过期的索引。索引过期后,相应的数据会被删除。
存储在过期索引字段的值必须是指定的时间类型,必须是ISODate或者ISODate数组,不能使用时间戳,否则不能被自动删除。
过期索引不能是复合索引。
删除时间是不精确的。(删除程序在后台每60秒执行一次)
--建立time索引,过期时间为30秒
db.imooc2.ensureIndex({time:1}, {expireAfterSeconds: 30});
-- 使用new Date()函数
db.imooc2.insert({time:new Date()});
--在imooc2中建立article的全文索引(一张表中只能建立一个全文索引)
db.imooc2.ensureIndex({"article":"text"});
--使用全文索引进行查找
db.imooc2.find({$text:{$search:"aa"}});
--使用全文索引进行多个条件查询(或查询)
db.imooc2.find({$text:{$search:"aa bb cc"}});
--多条件查询,但不包括cc
db.imooc2.find({$text:{$search:"aa bb -cc"}});
--多条件查询,与查询,使用“”
db.imooc2.find({$text:{$search:"\"aa\" \"bb\" \"cc\""}});
--全文索引结果的相似度排序sort{score:{$meta:"textScore"}}
db.imooc2.find({$text:{$search:"aa bb"}}, {score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}});
--自定义索引名字
db.imooc2.ensureIndex({x:1, y:1, z:1, m:1}, {name:"normal_index"});
--删除索引
db.imooc2.dropIndex("normal_index");
--构建唯一索引(字段值不能重复)
db.imooc2.ensureIndex({m:1, n:1}, {unique:true});
--查找m存在的数据
db.imooc2.find({m:{$exists:true}});
稀疏索引:如果集合中某条数据值为空的时候,不为该数据创建索引,mongodb默认创建的索引都是非稀疏索引
--为字段m创建稀疏索引
db.imooc2.ensureIndex({m:1}, {sparse:true});
--强制使用索引命令.hint
db.imooc2.find({m:{$extsts:true}}).hint("m_1");
地理位置索引:将一些点的位置存储在mongoDB中,创建索引后,可以按照位置来查找其他点。
2d索引:用于存储和查找平面上的点。
2dsphere:用于存储和查找球面上的点。
位置表示方式:经度【-180, 180】、纬度【-90, 90】
--为w字段创建地理位置索引,2d索引
db.location.ensureIndex({"w":"2d"});
--查找坐标位置附近的点(附近100个)
db.location.find({w:{$near:[1:1]}});
--查找离坐标位置10范围内的点
db.location.find({w:{$near:[1:1], $maxDistance:10}});
寻找特定范围(矩形、圆形、多边形)内的点$geoWithin
--寻找指定矩形范围内的点
db.location.find({w:{$geoWithin:{$box:[[0,0], [3,3]]}}})
--寻找指定圆形范围内的点
db.location.find({w:{$geoWithin:{$center:[[0,0], 5]}}})
--寻找指定多边形范围内的点
db.location.find({w:{$geoWithin:{$polygon:[[0,0], [0,1], [2,5], [6,1]]}}})
使用geoNear查找指定范围内的点(返回的信息更多,并对2dsphere索引可使用minDistance参数)
--查找location集合坐标1,2范围10内的点,限制返回1条数据(使用runCommand命令)
db.runCommand({geoNear:"location", near:[1,2], maxDistance:10, num:1})