mongodb的特殊集合和索引

166 阅读1分钟

6. 特殊索引和集合 :
1. 固定集合 : 集合的大小固定,如果向已满的固定集合中插入数据,集合会以队列的方式清空旧数据接纳新数据,固定集合在磁盘空间中时顺序写入的,写入速度非常快,固定集合不能被分片
2. 固定集合的创建 : db.createCollection("coll", {"capped" : true, "size" : 100000, "max" : 100, "autoIndexId" : true}); // autoIndexId决定是否创建_id索引, 没有_id索引不能复制
3. 固定集合排序 : 自然排序,返回的文档顺序就是文档在磁盘中的顺序 : db.coll.find().sort({"$natural" : -1})
4. 固定集合的循环游标 : 会不断读取新的信息,如果10分钟不用会自动释放;
5. 固定集合的TTL(具有生命周期的)索引 : 
1. 创建 : db.coll.ensureIndex({"lastUpdated" : 1}, {"expireAfterSesc" : 60*60*24});
2. 修改 : db.runCommand({"collMode" : "someapp.cache", "expireAfterSesc" : 3600})
6. 全文本索引 : 用于查询字符串文本, 每个集合中最多有一个全文本索引, 可以设置权重1~1 000 000 000
1. 启用 : db.adminCommand({"setParameter" : 1, "textSearchEnabled" : true});
2. 创建 : db.coll.ensureIndex({"tile" : "text", "desc" : "text", "auth" : "text"}, {"weights" : {"tile" : 3, "auth" : 5}})
3. 使用 : db.runCommand({"text" : "coll", "search" : "ass ddd ddd"})
4. 语法 : 默认使用OR连接的, 也可以精确匹配, 用双引号, 比使用OR慢 : db.runCommand({"text" : "coll", "search" : "\"ass ddd ddd\""} 
也可以两者同时使用 : db.runCommand({"text" : "coll", "search" : "\"ass ddd ddd\" hhh"}
也可以指定不允许出现的词(用-) : db.runCommand({"text" : "coll", "search" : "-aaa hhh"}
5. 优化 : db.coll.ensureIndex({"date" : 1, "post" : "text"}) // 可以先根据date缩小范围,再使用全文本索引
db.coll.ensureIndex({"auth" : "text", "post" : "text"}) // 复合全文本索引
db.coll.ensureIndex({"date" : 1, "auth" : "text", "post" : "text"})
6. 指定语言 : mongodb查找索引字段时会对字符串进行分词,将其减小为一个基本单元,不同语言的分词机制是不同的,所以要指定语言,默认为english :
db.coll.ensureIndex({"auth" : "text"}, {"default_language" : "chinese"}) // 指定语言
db.coll.insert({"name" : "abc", language : "chinese"}) // 插入时指定语言
7. 地理空间索引 : 常用的是2dsphere索引(地球表面地图)和2d索引(平面地图和时间连续的数据)
1. 2dsphere允许使用geojson的形式,用经纬度表示点线面等 :
{
"name" : "New York City",
"loc" : {
"type" : "Point",
"coordinates" : [50, 2]
}
}
{
"name" : "Hudson River",
"loc" : {
"type" : "Line",
"coordinates" : [[0,1], [0,2], [1,2]]
}
}
{
"name" : "New England",
"loc" : {
"type" : "Polygon",
"coordinates" : [[0,1], [0,2], [1,2]]
}
}
db.coll.ensureIndex({"loc" : "2dsphere"}) // 创建索引
var eastVillage = {
"type" : "Polygon",
"coordinates" : [
[-73.9917900, 40.7264100],
[-73.9917900, 40.7321400],
[-73.9829300, 40.7321400],
[-73.9829300, 40.7264100]
]
}
db.coll.find({"loc" : {"$geoIntersects" : {"$geometry" : eastVillage}}})
db.coll.find({"loc" : {"$within" : {"$geometry" : eastVillage}}})
db.coll.find({"loc" : {"$near" : {"$geometry" : eastVillage}}})
db.coll.ensureIndex("tags" : 1, location : "sdsphere") // 复合空间索引
db.coll.find({"loc" : {"$within" : {"$geometry" : eastVillage}}, "tags" : "pizza"})
2. 2d索引 : 用于扁平表面,不适合球面 默认范围 : -180~180
db.coll.ensureIndex({"title" : "2d"})
{
"name" : "Water Temple",
"title" : [32, 22]
}
db.coll.ensureIndex({"light-years" : "2d"}, {"min" : -1000, "max" : 1000})
db.coll.find({"title" : {"$near" : [20, 21]}}) // 点
db.coll.find({"title" : {"$within" : {"$box" : [[10, 20], [15, 30]]}}})
db.coll.find({"title" : {"$within" : {"$center" : [[12, 25], 5]}}})
db.coll.find({"title" : {"$within" " {"$polygon" : [[0, 20], [10, 0], [-10, 0]]}}})
8. 使用GridFS存储文件 : 大型二进制文件
1. 使用 :
echo "Hello world" > foo.txt
mongofiles -h 192.168.2.6:27017 put foo.txt
mongofiles -h 192.168.2.6:27017 list
rm foo.txt -f
mongofiles -h 192.168.2.6:27017 get foo.txt
cat foo.txt

db.files.distinct("filename")