Mongdb的索引备份以及和python交互

1. mongodb的索引

知识点

  • 掌握mongodb索引的创建,删除操作
  • 掌握mongodb查看索引的方法
  • 掌握mongodb创建联合索引的方法
  • 掌握mongodb创建唯一索引的方法

1.1 为什么mongdb需要创建索引

  • 加快查询速度
  • 进行数据的去重

1.2 mongodb创建简单的索引方法

  • 语法:

    • db.集合.ensureIndex({属性:1}),1表示升序, -1表示降序
    • db.集合.createIndex({属性:1})
    • 上面两个命令效果等价
  • 具体操作:db.db_name.ensureIndex({name:1})

1.3 创建索引前后查询速度对比

测试:插入10万条数据到数据库中 插入数据:

for(i=0;i<100000;i++){db.t255.insert({name:'test'+i,age:i})}

创建索引前:

db.t1.find({name:'test10000'})
db.t1.find({name:'test10000'}).explain('executionStats')

创建索引后:

db.t255.ensureIndex({name:1})
db.t1.find({name:'test10000'}).explain('executionStats')


1.4 索引的查看

默认情况下_id是集合的索引

查看方式:db.collection_name.getIndexes()

添加索引前:

> db.test2000.insert({"name":"hello",age:20})
WriteResult({ "nInserted" : 1 })
> db.test2000.find()
{ "_id" : ObjectId("5ae0232f625b9ddd91a0e7ae"), "name" : "hello", "age" : 20 }
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    }
]

添加name为索引后:

> db.test2000.ensureIndex({name:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test2000.test2000"
    }
]

1.5 mongodb创建唯一索引

在默认情况下mongdb的索引字段的值是可以相同的,仅仅能够提高查询速度

添加唯一索引的语法:

db.collection_name.ensureIndex({"name":1},{"unique":true})

使用普通索引的效果如下:

> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test2000.test2000"
    }
]
> db.test2000.insert({name:"hello",age:40})
WriteResult({ "nInserted" : 1 })
> db.test2000.find()
{ "_id" : ObjectId("5ae0232f625b9ddd91a0e7ae"), "name" : "hello", "age" : 20 }
{ "_id" : ObjectId("5ae02421625b9ddd91a0e7af"), "name" : "hello", "age" : 30 }
{ "_id" : ObjectId("5ae02432625b9ddd91a0e7b0"), "name" : "hello", "age" : 40 }

添加age为唯一索引之后:

> db.test2000.createIndex({age:1},{unique:true})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
}
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "unique" : true,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test2000.test2000"
    }
]
> db.test2000.insert({"name":"world",age:20})
WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 11000,
        "errmsg" : "E11000 duplicate key error collection: test2000.test2000 index: age_1 dup key: { : 20.0 }"
    }
})

1.6 删除索引

语法:db.t1.dropIndex({'索引名称':1})

> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "unique" : true,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test2000.test2000"
    }
]
> db.test2000.dropIndex({age:1})
{ "nIndexesWas" : 3, "ok" : 1 }
> db.test2000.dropIndex({name:1})
{ "nIndexesWas" : 2, "ok" : 1 }
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    }
]

1.6 建立复合索引

在进行数据去重的时候,可能用一个字段来保证数据的唯一性,这个时候可以考虑建立复合索引来实现。

例如:抓全贴吧信息,如果把帖子的名字作为唯一索引对数据进行去重是不可取的,因为可能有很多帖子名字相同

建立复合索引的语法:db.collection_name.ensureIndex({字段1:1,字段2:1})

> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    }
]
> db.test2000.createIndex({name:1,age:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1,
            "age" : 1
        },
        "name" : "name_1_age_1",
        "ns" : "test2000.test2000"
    }
]

1.7 建立索引注意点

  • 根据需要选择是否需要建立唯一索引
  • 索引字段是升序还是降序在单个索引的情况下不影响查询效率,但是带复合索引的条件下会有影响

    例如:在进行查询的时候如果字段1需要升序的方式排序输出,字段2需要降序的方式排序输出,那么此时复合索引的建立需要把字段1设置为1,字段2设置为-1

2. mongodb的备份和恢复

知识点

  • 掌握mongdb的备份和恢复命令

    2.1 备份

    备份的语法:mongodump -h dbhost -d dbname -o dbdirectory
  • -h: 服务器地址, 也可以指定端⼝号
  • -d: 需要备份的数据库名称
  • -o: 备份的数据存放位置, 此⽬录中存放着备份出来的数据

示例:mongodump -h 192.168.196.128:27017 -d test1 -o ~/Desktop/test1bak

2.2 恢复

恢复语法:mongorestore -h dbhost -d dbname --dir dbdirectory

  • -h: 服务器地址
  • -d: 需要恢复的数据库实例
  • --dir: 备份数据所在位置

示例:mongorestore -h 192.168.196.128:27017 -d test2 --dir ~/Desktop/test1bak/test1

 

mongodb和python交互

目标

  • 掌握mongdb和python交互的增删改查的方法

1. mongdb和python交互的模块

pymongo 提供了mongdb和python交互的所有方法 安装方式: pip install pymongo

2. 使用pymongo

  1. 导入pymongo并选择要操作的集合 数据库和集合乜有会自动创建

     from pymongo import MongoClient
     client = MongoClient(host,port)
     collection = client[db名][集合名]
    
  2. 添加一条数据

    ret = collection.insert_one({"name":"test10010","age":33})
     print(ret)
    
  3. 添加多条数据

     item_list = [{"name":"test1000{}".format(i)} for i in range(10)]
         #insert_many接收一个列表,列表中为所有需要插入的字典
     t = collection.insert_many(item_list)
    
  4. 查找一条数据

     #find_one查找并且返回一个结果,接收一个字典形式的条件
     t = collection.find_one({"name":"test10005"})
     print(t)
    
  5. 查找全部数据

    结果是一个Cursor游标对象,是一个可迭代对象,可以类似读文件的指针,但是只能够进行一次读取

     #find返回所有满足条件的结果,如果条件为空,则返回数据库的所有
     t = collection.find({"name":"test10005"})
         #结果是一个Cursor游标对象,是一个可迭代对象,可以类似读文件的指针,
     for i in t:
         print(i)
     for i in t: #此时t中没有内容
         print(i)
    
  6. 更新一条数据 注意使用$set命令

     #update_one更新一条数据
     collection.update_one({"name":"test10005"},{"$set":{"name":"new_test10005"}})
    
  7. 更行全部数据

     # update_one更新全部数据
     collection.update_many({"name":"test10005"},{"$set":{"name":"new_test10005"}})
    
  8. 删除一条数据

     #delete_one删除一条数据
     collection.delete_one({"name":"test10010"})
    
  9. 删除全部数据

     #delete_may删除所有满足条件的数据
     collection.delete_many({"name":"test10010"})

 

# coding=utf-8
from pymongo import MongoClient

#建立和mongodb的连接
client = MongoClient(host="127.0.0.1",port=27017)

#选择要操作的数据库和集合
collection = client["test100"]["t1"]


#插入数据的操作
# collection.insert_one({"name":10086,"age":20})
# item_list = [{"name":"py_%s"%i,"age":i} for i in range(10)]
# print(item_list)
# collection.insert_many(item_list)

#查找数据
# ret = collection.find_one({"name":"py_0"})
# print(type(ret))
# print(ret)
# ret2 = collection.find({"name":"py_0"})
# print(ret2)  #Cursor,能够进行比遍历
# for i in ret2:
#     print(i)
#
# for i in ret2:
#     print("*"*100)
# ret2 = list(ret2)
# print(ret2)

#更新
# collection.update_one({"name":"py_0"},{"$set":{"age":1000}})
# print(list(collection.find({"name":"py_0"})))

# collection.update_many({"name":"py_1"},{"$set":{"age":1000}})
# print(list(collection.find({"name":"py_1"})))

#删除
# collection.delete_one({"name":"py_0"})
# print(list(collection.find({"name":"py_0"})))

# collection.delete_many({"name":"py_1"})
# print(list(collection.find({"name":"py_1"})))

 

 

 

posted @ 2018-08-13 22:12  __wu__yc  阅读(141)  评论(0编辑  收藏  举报