Recipe 4-1. Working with Indexes(使用索引)
准备数据
db.employee.insert({empId:1,empName:"John",state:"KA",country:"India"})
db.employee.insert({empId:2,empName:"Smith",state:"CA",country:"US"})
db.employee.insert({empId:3,empName:"James",state:"FL",country:"US"})
db.employee.insert({empId:4,empName:"Josh",state:"TN",country:"India"})
db.employee.insert({empId:5,empName:"Joshi",state:"HYD",country:"India"})
1.创建索引
db.employee.createIndex({empId:1})
2.创建多个域的索引
db.employee.createIndex({empId:1,empName:1})
3.查看索引
db.employee.getIndexes()
4.删除指定索引
db.employee.dropIndex({empId:1,empName:1})
5.删除所有索引
db.employee.dropIndexes()
6.注意_id是默认索引,是不能删除
Recipe 4-2. Index Types(索引类型)
准备数据
db.employeeproject.insert({empId:1001,empName:"John",projects:["Hadoop","MongoDB"]})
db.employeeproject.insert({empId:1002,empName:"James",projects:["MongoDB","Spark"]})
1.多个key的索引--即指定数据中的某个作为索引
db.employeeproject.createIndex({projects:1})
2.文本索引
有这样的数据
db.post.insert({
"post_text": "Happy Learning",
"tags": [
"mongodb",
"10gen"
]
})
创建索引:db.post.createIndex({post_text:"text"})
查询:db.post.find({$text:{$search:"Happy"}})
3.Hashed索引
有这样的数据
db.user.insert({userId:1,userName:"John"})
db.user.insert({userId:2,userName:"James"})
db.user.insert({userId:3,userName:"Jack"})
创建Hashed索引:db.user.createIndex( { userId: "hashed" } )
4.2dsphere索引
有这样的数据
db.schools.insert( {name: "St.John's School",location: { type: "Point", coordinates: [ -73.97, 40.77 ] },} );
db.schools.insert( {name: "St.Joseph's School",location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] },} );
db.schools.insert( {name: "St.Thomas School",location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },} );
创建索引:db.schools.createIndex( { location : "2dsphere" } )
查询(位置):
db.schools.find({location:{$near:{$geometry: { type:"Point", coordinates: [ -73.9667, 40.78 ] },$minDistance:500,$maxDistance: 1500}}})
Recipe 4-3. Index Properties(索引属性)
1.TTL索引
准备数据
db.credit.insert({credit:16})
db.credit.insert({credit:18})
db.credit.insert({credit:12})
创建索引:db.credit.createIndex( { credit: 1 }, { expireAfterSeconds: 35 } );
2.Unique索引
准备数据
db.student.insert({_id:1,studid:101,studname:"John"})
db.student.insert({_id:2,studid:102,studname:"Jack"})
db.student.insert({_id:3,studid:103,studname:"James"})
创建索引:db.student.createIndex({"studid":1}, {unique:true})
插入数据会报错:db.student.insert([{_id:1,studid:101,studname:"John"}])
3.Partial索引
可以使用的操作符:$exists,$gt,$gte,$lt,$lte,$type,$and
准备数据
db.person.insert({personName:"John",age:16})
db.person.insert({personName:"James",age:15})
db.person.insert({personName:"John",hobbies:["sports","music"]})
创建索引
db.person.createIndex( { age: 1},{partialFilterExpression: {age: { $gt: 15 }}})
查询:db.person.find( { age: { $gt: 15 } } )
4.Sparse索引
准备数据
db.person.insert({personName:"John",age:16})
db.person.insert({personName:"James",age:15})
db.person.insert({personName:"John",hobbies:["sports","music"]})
创建索引
db.person.createIndex( { age: 1 }, { sparse: true } );
查询:db.person.find().hint( { age: 1 } ).count();
db.person.find().count();
Recipe 4-4. Indexing Strategies(索引策略)
1.创建索引用于查询
db.employee.createIndex({empId:1})
db.employee.createIndex({empId:1,empName:1})
2.使用索引来排序结果
db.employee.createIndex({empId:1})
db.employee.find().sort({empId:1})
3.排序多个域
db.employee.createIndex({empId:1,empName:1})
db.employee.find().sort({empId:1,empName:1})
4.查询索引可以使用的大小
db.employee.totalIndexSize()