system.indexes下包含所有索引信息
自动创建的有每个集合的_id和shard key索引
mongos> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc1" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc0" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1, "int2" : 1 }, "name" : "int1_1_int2_1", "ns" : "test.doc0" }
{ "v" : 1, "key" : { "datee" : 1 }, "name" : "datee_1", "ns" : "test.doc1" }
{ "v" : 1, "key" : { "_id" : "hashed" }, "name" : "_id_hashed", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1 }, "name" : "int1_1", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc1" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc0" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1, "int2" : 1 }, "name" : "int1_1_int2_1", "ns" : "test.doc0" }
{ "v" : 1, "key" : { "datee" : 1 }, "name" : "datee_1", "ns" : "test.doc1" }
{ "v" : 1, "key" : { "_id" : "hashed" }, "name" : "_id_hashed", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1 }, "name" : "int1_1", "ns" : "test.doc2" }
创建单列索引
mongos> db.doc2.createIndex({int1: 1});
{
"raw" : {
"rs0/rs0-1:4021,rs0-2:4022" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1427783921, 1),
"electionId" : ObjectId("551a2ae2990be7dcbb04bc36")
}
},
"rs1/rs1-1:4031,rs1-2:4032" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1427783920, 1),
"electionId" : ObjectId("551a2ae0533749e2a0afdff5")
}
},
"rs2/rs2-1:4041,rs2-2:4042" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1427783920, 1),
"electionId" : ObjectId("551a2ae17178e928a24df7d7")
}
},
"rs3/rs3-1:4051,rs3-2:4052" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1427783921, 1),
"electionId" : ObjectId("551a2aec443124be35cfec75")
}
}
},
"ok" : 1
}
{
"raw" : {
"rs0/rs0-1:4021,rs0-2:4022" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1427783921, 1),
"electionId" : ObjectId("551a2ae2990be7dcbb04bc36")
}
},
"rs1/rs1-1:4031,rs1-2:4032" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1427783920, 1),
"electionId" : ObjectId("551a2ae0533749e2a0afdff5")
}
},
"rs2/rs2-1:4041,rs2-2:4042" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1427783920, 1),
"electionId" : ObjectId("551a2ae17178e928a24df7d7")
}
},
"rs3/rs3-1:4051,rs3-2:4052" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1427783921, 1),
"electionId" : ObjectId("551a2aec443124be35cfec75")
}
}
},
"ok" : 1
}
mongos> db.addr.insert(
... {
... "name": "John Doe",
... " address": {
... "street": "Main",
... " zipcode": "53511",
... "state": "WI"
... }
... });
WriteResult({ "nInserted" : 1 })
... {
... "name": "John Doe",
... " address": {
... "street": "Main",
... " zipcode": "53511",
... "state": "WI"
... }
... });
WriteResult({ "nInserted" : 1 })
在内嵌对象上创建索引
db.addr.createIndex( { " address.zipcode": 1 } );
db.addr.createIndex( { " address.zipcode": 1 } );
甚至直接为整个内嵌文档创建索引
db.addr.createIndex( {
address: 1 });
复合索引
mongos> db.addr.insert(
... {
... id: 1,
... name: "a"
... }
... );
... {
... id: 1,
... name: "a"
... }
... );
db.addr.createIndex({id:1, name:1});
复合索引的
限制
符合索引的某个键可以是数组
mongos> db.addr.insert(
... {
... id: [1,2],
... name: "a"
... }
... );
WriteResult({ "nInserted" : 1 })
mongos> db.addr.insert({
... id: 1,
... name: ["x","y"]
... }
... );
WriteResult({ "nInserted" : 1 })
但是不能有两个键的值都是数组
mongos> db.addr.insert({
... id: [1,2],
... name: ["x","y"]
... }
... );
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 10088,
"errmsg" : "
cannot index parallel arrays [name] [id]"
}
})
multikey索引
mongos> db.addr.insert(
... {
... id: 1,
... name: "a",
... zips: [
... {zipcode: 111},
... {zipcode: 222},
... {zipcode: 333}
... ]
... }
... );
... {
... id: 1,
... name: "a",
... zips: [
... {zipcode: 111},
... {zipcode: 222},
... {zipcode: 333}
... ]
... }
... );
上面addr的zips,是一个数组,里面有多个zipcode。可以用
db.addr.createIndex({'zips.zipcode': 1});
multikey索引的限制
shard key无法做multikey
哈希索引
和oracle一样,不支持范围扫描
db.aaa.createIndex( { a: "hashed" } )
TTL索引
数据插入一定时间后自动删除数据,单位是秒。
索引的字段必须是日期型,或包含日期类型的字段。
只支持单列索引。
db.doc2.createIndex({dt:1}, {expireAfterSeconds: 10})
db.doc2.insert({int1:99999, dt:new Date()})
...
...
上面设置的是10秒,但不一定真的能在10秒的时候删除。
sparse索引
这种索引只存储那些包含索引字段的文档,包括空值的。
db.doc2.createIndex( { "int2": 1 }, {
sparse: true
} )
后台创建索引
默认情况下创建索引会阻塞数据库上所有操作,除非指定后台创建。
指定后台创建时,只有当前session会被阻塞。
进行后台创建时,无法进行其它与这个集合相关的管理类操作。
db.doc2.createIndex( { int3: 1}, {
background: true} )
删除索引
mongos> db.system.indexes.find({ns: "test.doc2"})
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "_id" : "hashed" }, "name" : "_id_hashed", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1 }, "name" : "int1_1", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "_id" : "hashed" }, "name" : "_id_hashed", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1 }, "name" : "int1_1", "ns" : "test.doc2" }
db.doc2.dropIndex("int1_1")
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26239116/viewspace-1485420/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26239116/viewspace-1485420/