自动创建的有每个集合的_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" }
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
}
mongos> db.addr.insert(
... {
... "name": "John Doe",
... "
address": {
... "street": "Main",
... "
zipcode": "53511",
... "state": "WI"
... }
... });
WriteResult({ "nInserted" : 1 })
在内嵌对象上创建索引 db.addr.createIndex( { "
address.zipcode": 1 } );
db.addr.createIndex( {
address: 1 });
mongos> db.addr.insert(
... {
... id: 1,
... name: "a"
... }
... );
db.addr.createIndex({id:1, name:1});
WriteResult({ "nInserted" : 1 })
WriteResult({ "nInserted" : 1 })
"errmsg" : "
cannot index parallel arrays [name] [id]"
mongos> db.addr.insert(
... {
... id: 1,
... name: "a",
... zips: [
... {zipcode: 111},
... {zipcode: 222},
... {zipcode: 333}
... ]
... }
... );
上面addr的zips,是一个数组,里面有多个zipcode。可以用 db.addr.createIndex({'zips.zipcode': 1});
db.aaa.createIndex( { a: "hashed" } )
db.doc2.createIndex({dt:1}, {expireAfterSeconds: 10})
db.doc2.insert({int1:99999, dt:new Date()})
上面设置的是10秒,但不一定真的能在10秒的时候删除。 这种索引只存储那些包含索引字段的文档,包括空值的。 db.doc2.createIndex( { "int2": 1 }, {
sparse: true } )
默认情况下创建索引会阻塞数据库上所有操作,除非指定后台创建。 进行后台创建时,无法进行其它与这个集合相关的管理类操作。 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" }
db.doc2.dropIndex("int1_1") 来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26239116/viewspace-1485420/,如需转载,请注明出处,否则将追究法律责任。