增加索引:1(ascending),-1(descending)
db.getCollection('product').getIndexes()
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "netkiller.product" }, { "v" : 2, "unique" : true, "key" : { "uuid" : 1 }, "name" : "uuid", "ns" : "netkiller.product", "sparse" : true }, { "v" : 2, "key" : { "nfc" : 1 }, "name" : "nfc", "ns" : "netkiller.product" }, { "v" : 2, "unique" : true, "key" : { "qrcode" : 1 }, "name" : "qrcode", "ns" : "netkiller.product", "sparse" : true }, { "v" : 2, "key" : { "memberId" : 1 }, "name" : "memberId", "ns" : "netkiller.product" }, { "v" : 2, "unique" : true, "key" : { "transactionId" : 1 }, "name" : "transactionId", "ns" : "netkiller.product", "sparse" : true } ]
查看索引信息
db.logging.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "logging.logging", "name" : "_id_" } ]
查看索引名與排序方式
db.getCollection('member').getIndexKeys(); [ { "_id" : 1 }, { "mobile" : 1 }, { "username" : 1 }, { "wechat" : 1 } ]
增加索引
db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
索引子對象
db.logging.users.ensureIndex({address.city:1})
db.getCollection('product').dropIndex("memberId")
根據索引名刪除索引
> db.logging.users.dropIndex('name_1') { "nIndexesWas" : 2, "ok" : 1 } > db.logging.users.getIndexKeys() [ { "_id" : 1 } ]
db.members.createIndex( { "user_id": 1 }, { unique: true } )
> db.apple.createIndex({"devicetoken":1},{unique: true}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }