Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

47.8. Unique Indexes

增加索引:1(ascending),-1(descending)

47.8.1. 索引管理

增加索引

db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
		

索引子对象

		
db.logging.users.ensureIndex({address.city:1})
		
		

查看索引信息

db.logging.getIndexes()
[
	{
		"v" : 1,
		"key" : {
			"_id" : 1
		},
		"ns" : "logging.logging",
		"name" : "_id_"
	}
]
		

db.logging.users.getIndexKeys()

[ { "_id" : 1 }, { "name" : 1 } ]
		

根据索引名删除索引

		
> db.logging.users.dropIndex('name_1')
{ "nIndexesWas" : 2, "ok" : 1 }

> db.logging.users.getIndexKeys()
[ { "_id" : 1 } ]
		
		

47.8.1.1. 唯一索引

db.members.createIndex( { "user_id": 1 }, { unique: true } )
			
			
> db.apple.createIndex({"devicetoken":1},{unique: true})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
			
			
comments powered by Disqus