mongodb实践 3

#创建索引

*索引说白了是一种数据结构,这种数据结构通常用在优化检索效率上,mongodb的索引通过B-tree实现。

*获取things的索引@

> db.things.getIndexes()

		[
			{
				"name" : "_id_",
				"ns" : "test.things",
				"key" : {
					"_id" : 1
				}
			}
		]
 

_id是所有document都有的索引。

*在i上创建一个索引,之后things会有两个索引@

> db.things.ensureIndex({i:1});

		> db.things.getIndexes()
		[
			{
				"name" : "_id_",
				"ns" : "test.things",
				"key" : {
					"_id" : 1
				}
			},
			{
				"_id" : ObjectId("4ee86f3d9027000000001ba5"),
				"ns" : "test.things",
				"key" : {
					"i" : 1
				},
				"name" : "i_1"
			}
		]
 

说明一下:mongodb官网说@

写道

The name of an index is generated by concatenating the names of the indexed fields and their direction (i.e., 1 or -1 for ascending or descending). Index names (including their namespace/database), are limited to 128 characters.
 

也就是说命令db.things.ensureIndex({i:1});里面的1会与i连接在一起生成索引的名字,1代表升序、-1代表降序,索引名字的长度不可超过128个字符,事实上聚集(collection)也有这样的限制。

ensureIndex()函数的说明是这样的:

db.things.ensureIndex(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups

 

写道

option values default
background true/false false
dropDups true/false false
unique true/false false
sparse true/false false
v index version. 0 = pre-v2.0, 1 = smaller/faster (current) 1 in v2.0. Default is used except in unusual situations.
 

mongodb创建索引是可以使用.号,下面是官网给出的例子@

db.factories.insert( { name: "xyz", metro: { city: "New York", state: "NY" } } );

		db.factories.ensureIndex( { "metro.city" : 1, "metro.state" : 1 } );
		// these queries can use the above index:
		db.factories.find( { "metro.city" : "New York", "metro.state" : "NY" } );
		db.factories.find( { "metro.city" : "New York" } );
		db.factories.find().sort( { "metro.city" : 1, "metro.state" : 1 } );
		db.factories.find().sort( { "metro.city" : 1 } )
 

*创建稀疏索引Sparse Indexes@

> db.people.ensureIndex({title : 1}, {sparse : true})

		> db.people.save({name:"Jim"})
		> db.people.save({name:"Sarah", title:"Princess"})
		> db.people.find()
		{ "_id" : ObjectId("4de6abd5da558a49fc5eef29"), "name" : "Jim" }
		{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }
		> db.people.find().sort({title:1}) // only 1 doc returned because sparse
		{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }
		> db.people.dropIndex({title : 1})
		{ "nIndexesWas" : 2, "ok" : 1 }
		> db.people.find().sort({title:1}) // no more index, returns all documents
		{ "_id" : ObjectId("4de6abd5da558a49fc5eef29"), "name" : "Jim" }
		{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }
 

当使用Sparse Index的时候,某个没有title域的文档将不能被检索出。

*创建唯一索引@

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

		db.things.save({lastname: "Smith"});
		// Next operation will fail because of the unique index on firstname.
		db.things.save({lastname: "Jones"});
 

如果已有数据中有重复数据域,则在这个域上无法创建唯一索引,通过dropDups参数可以强制创建索引,使用该参数在创建索引的时候重复的数据会被无情的删除。

写道

A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate values, add the dropDups option.
db.things.ensureIndex({firstname : 1}, {unique : true, dropDups : true})
 

*注意mongodb支持复合索引@

例如db.things.ensureIndex({i: 1,i2: 1});与db.things.ensureIndex({i: 1});db.things.ensureIndex({i2: 1});是完全不同的!

*删除索引@

db.collection.dropIndexes();

		db.collection.dropIndex({i: 1})
 

*也可以通过命令删除@

// remove index with key pattern {y:1} from collection foo

		db.runCommand({dropIndexes:'foo', index : {y:1}})
		// remove all indexes:
		db.runCommand({dropIndexes:'foo', index : '*'})
 

#使用特权指令

*关闭数据库@

> use admin;

		> db.runCommand("shutdown");
 

或者,如果不是admin可以@

> db._adminCommand("shutdown");

 

或者这样@

> db.shutdownServer();

 

#复制数据库

*复制数据库mydb到test@

> db.copyDatabase("mydb", "test", "localhost");

		{ "ok" : 1 }
 

#关于MapReduce

官方例子1@

> m = function() { emit(this.user_id, 1); }

		> r = function(k,vals) { return 1; }
		> res = db.events.mapReduce(m, r, { query : {type:'sale'} });
		> // or in v1.8+:
		> // res = db.events.mapReduce(m, r, { query : {type:'sale'}, out : 'example1' });
		> db[res.result].find().limit(2)
		{ "_id" : 8321073716060 , "value" : 1 }
		{ "_id" : 7921232311289 , "value" : 1 }

		> r = function(k,vals) {
		...     var sum=0;
		...     for(var i in vals) sum += vals[i];
		...     return sum;
		... }
 

官方例子2@

$ ./mongo

		> db.things.insert( { _id : 1, tags : ['dog', 'cat'] } );
		> db.things.insert( { _id : 2, tags : ['cat'] } );
		> db.things.insert( { _id : 3, tags : ['mouse', 'cat', 'dog'] } );
		> db.things.insert( { _id : 4, tags : []  } );

		> // map function
		> m = function(){
		...    this.tags.forEach(
		...        function(z){
		...            emit( z , { count : 1 } );
		...        }
		...    );
		...};

		> // reduce function
		> r = function( key , values ){
		...    var total = 0;
		...    for ( var i=0; i<values.length; i++ )
		...        total += values[i].count;
		...    return { count : total };
		...};

		> res = db.things.mapReduce(m, r, { out : "myoutput" } );
		> res
		{
			"result" : "myoutput",
			"timeMillis" : 12,
			"counts" : {
				"input" : 4,
				"emit" : 6,
				"output" : 3
			},
			"ok" : 1,
		}
		> db.myoutput.find()
		{"_id" : "cat" , "value" : {"count" : 3}}
		{"_id" : "dog" , "value" : {"count" : 2}}
		{"_id" : "mouse" , "value" : {"count" : 1}}

		> db.myoutput.drop()
 

关于MR网上有许多细致的分析,本人感觉MR与分治的思想相似。

猜你喜欢

转载自shenbai.iteye.com/blog/1313737
今日推荐