#创建索引
*索引说白了是一种数据结构,这种数据结构通常用在优化检索效率上,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与分治的思想相似。
分享到:
相关推荐
mongodb实践,mongodb实践,mongodb实践,mongodb实践,mongodb实践,mongodb实践,mongodb实践,mongodb实践,mongodb实践,mongodb实践,mongodb实践。
MongoDB简介与实践
Mongodb是主流的NOSQL数据库之一,Mongodb最佳实践,详细介绍了Mongodb使用以及底层原理,和运维管理; 1.Mongodb数据结构,以及存储方式 2.增删改查使用,分页,排序,投影,以及多种扩展使用 3.丰富查询语句,比如...
MongoDB实践
视觉中国的MongoDB应用实践 视觉中国的MongoDB应用实践
关系型数据库迁移MongoDB实践.pptx
MongoDB 性能最佳实践.
MongoDB应用实践 MongoDB应用实践 MongoDB应用实践 MongoDB应用实践
-涵盖了将数据从RDBMS移至MongoDB时的最佳实践和注意事项 MongoDB现代化记分卡 -使用它来确定哪些现有的旧版应用程序适合迁移到MongoDB 实践Medical_Claims_RDBMS_ERD和Medical_Claims_MongoDB -此方案适用于...
day3:MongoDB增删改查操作实践 day4:教你学会MongoDB聚合操作 day5:索引的特性及应用 day6:MongoDB实例搭建仓位管理API day7:数据模型优化及设计 day8:复制集介绍及演练 day9:海量数据分片 day10:数据库认证与授权...
高可用的MongoDB集群部署实践
mongodb 最佳实践,官方团队指导,内容非常详细丰富,适合指导阅读
MongoDb是一种非关系型的数据库(Nosql)相比关系型数据库其中之一的好处是它有灵活 的拓展字段, 二、MongoDB结构 MongoDB的数据模型是面向文档性的,这里的文档指的是类似json的机构。 MongoDB层次机构如下图,...
网易游戏MongoDB数据备份与恢复实践.pdf
《MongoDb in Action》讲解mongodb方面的书籍,比较详细,配合那个权威指南
MongoDB热温数据的最佳实践.pdf
mongdodb实践总结(中文版),一份工作中常遇到mongodb问题的总结文档
MongoDB热温数据的最佳实践.pptx
Node.js 实践: 使用 MongoDB 存储数据 MongoDB命令行工具的使用 在项目中如何有效组织和使用Mongoose
MongoDB数据库优化实践.pptx