8-mongodb

lesson08-mongodb

## pip换源


+ 镜像源
+ 清华: https://pypi.tuna.tsinghua.edu.cn/simple
+ 豆瓣: http://pypi.douban.com/simple/
+ 阿里: http://mirrors.aliyun.com/pypi/simple/
+ http://pypi.mirrors.ustc.edu.cn/simple/


+ **pip install 库名 -i 镜像源地址**
+ pip install pymysql -i https://pypi.tuna.tsinghua.edu.cn/simple



-------------------------------------------------------------
#视频1-作业讲解:

 1 # data = {
 2 #     'name':'juhao',
 3 #     'age': 18,
 4 #     'feature': ['高', '富', '帅']
 5 # }
 6 # 在程序中将这个字典转换为json对象存入文件内,
 7 # 并从这个文件读取出‘帅’这个字符打印出来。
 8 import json
 9 data = {
10     'name':'juhao',
11     'age': 18,
12     'feature': ['', '', '']
13 }
14 
15 with open('test.json','w') as fp:
16     json.dump(data,fp)
17 
18 fp = open('test.json','r')
19 
20 result =json.load(fp)
21 print(result)
22 print(result['feature'][2])

#运行:
{'name': 'juhao', 'age': 18, 'feature': ['高', '富', '帅']}

//查看
pyvip@Vip:~/20180606py_case/进阶$ cat test.json
{"name": "juhao", "feature": ["\u9ad8", "\u5bcc", "\u5e05"], "age": 18}

-------------------------------------------------------------

 1 #视频2-mongodb概念
 2 monggodb中数据类似json,文档形数据库
 3 
 4 xshell中:
 5 mongo     //进入mongodb
 6 exit    //退出
 7 
 8 
 9 -------------------------------------------------------------
10 #视频3-mongodb-库级操作语句
11 show dbs   //显示库
12 use student    //存在则使用,不存在则创建student库
13 db      // 查看所在库
14 db.dropDatabase()   //删除当前所在库
15 
16 集合(表)操作语句
17 show collections    //查看当前数据库的集合
18 db.createCollection('name')    //集合创建
19 db.集合名称.drop()   //删除集合
20 
21 
22 -------------------------------------------------------------
23 #视频4-mongodb-文档插入命令
24 //插入文档时,如果不指定_id参数,MongoDB会为文档分配一个唯一的ObjectId
25 db.table.insert({name:'juhao',age:20})   //插入数据
26 
27 例: db.table.insert({_id: 1, name: 'juhao', age:20})
28 
29 //插入多条
30 db.table.insert([
31     {name:'juhao', sex:'', age:18},
32     {name:'nanbei', sex:'', age:18},
33     {name:'budong', sex:'', age:18},
34 ])
35 
36 -------------------------------------------------------------
37 #视频5-mongodb-文档查询命令
38 db.table.find()    //查看数据
39 db.table.find().pretty()   //分类
40 db.table.insert().pretty()    //41 db.table.find({'age':20})   //指定查看age=20的数据
42 db.table.find({'age':{$ne:20}})   //指定查看数据---age不等于20的
43 //高级查询
44 $ne     不等于
45 $gt        大于
46 $lt        小于
47 $gte    大于等于
48 $lte    小于等于
49 and条件            {$and:[{expression1}, {expression1}, ...]   }
50 or条件            {$or:[{expression1}, {expression1}, ...]   }
51 
52 //where name='juhao' and age>18  ,查找数据,name=juhao,age=20
53 db.table.find(
54     {
55         $and:[{name:'juhao'},{age:{$gt:18}}]
56     }
57 )
58 
59 -------------------------------------------------------------
60 #视频6-mongodb-文档修改命令
61 db.集合名称.update(<query>, <update>, {multi: <boolean>} )
62 db.table.update({sex:''},{age:20})    //全文档替换:把条件是sex=男的,age变成20
63 db.table.update({name:'juhao'}, {$set: {age:666, sex: 'xx'}} )    //指定属性更新: { $set: {age:20}  }
64 db.table.update({sex:''}, {$set:{sex:''}}, { multi:true} )     //更新多条: { multi: ture } 
65 
66 
67 
68 -------------------------------------------------------------
69 #视频7-mongodb-文档删除命令
70 db.集合名称. remove(<query>, {justOne: <boolean>)
71 db.table.remove({sex: '男’} )         //删除满足条件的所有数据:
72 db.table.remove({})                删除集合所有的文档
73 db.table.remove( {sex:’男'},{$set:{sex:''}},{ multi:true} )       //只删除一条数据: { justOne: true } 
74 
75 
76 -------------------------------------------------------------
77 #视频8-mongodb-程序操作mongodb
78 1,安装python包:pip install pymongo
79 2,建立连接: client= pymongo.MongoClient('127.0.0.1', 27017)
80 3,指定数据库:db=client[ 数据库名 ]
81 4,指定集合:collection=db [ 集合名]

#08程序连接mongodb3.py:

 1 import pymongo
 2 
 3 conn= pymongo.MongoClient('127.0.0.1',27017)
 4 print(conn.database_names())  #获得所有库的名字
 5 
 6 db =conn['tanzhou']    #指定数据库
 7 print(db.collection_names())   #有哪些集合
 8 
 9 
10 collection = db['table']  #指定集合
11 
12 
13 #准备插入的数据
14 message ={
15     'name':'juhao',
16     'age':21
17 }
18 
19 collection.insert(message)  #插入数据
20 
21 result= collection.find()   #查看数据
22 for data in result:
23     print(data)
24 
25 collection.delete_one({'name':'juhao'})  #删除一条 name=juhao
26 collection.update_many({'name':'juhao'},{'$set':{'age':20}})   #修改所有name=juhao,把age=20
27 
28 print('-----------------------------------------------')
29 print('-----------------------------------------------')
30 print('-----------------------------------------------')
31 result= collection.find()   #查看数据
32 for data in result:
33     print(data)

#运行:
['local', 'tanzhou', 'admin', 'test']
['table', 'system.indexes']
{'_id': ObjectId('5ca3672e1e46fc035c57d1ed'), 'age': 20.0}
{'_id': ObjectId('5ca3672e1e46fc035c57d1ee'), 'name': 'nanbei', 'age': 18.0, 'sex': '女'}
{'_id': ObjectId('5ca3672e1e46fc035c57d1ef'), 'name': 'budong', 'age': 18.0, 'sex': '女'}
{'_id': 2.0, 'name': 'juhao222', 'age': 20.0}
{'_id': 1.0, 'name': 'juhao', 'age': 666.0, 'sex': 'xx'}
{'_id': ObjectId('5ca379998a6e9e095b36641a'), 'name': 'juhao', 'age': 21}
{'_id': ObjectId('5ca37cde8a6e9e0967ae7f89'), 'name': 'juhao', 'age': 21}
{'_id': ObjectId('5ca37d1b8a6e9e09768d0348'), 'name': 'juhao', 'age': 21}
-----------------------------------------------
-----------------------------------------------
-----------------------------------------------
{'_id': ObjectId('5ca3672e1e46fc035c57d1ed'), 'age': 20.0}
{'_id': ObjectId('5ca3672e1e46fc035c57d1ee'), 'name': 'nanbei', 'age': 18.0, 'sex': '女'}
{'_id': ObjectId('5ca3672e1e46fc035c57d1ef'), 'name': 'budong', 'age': 18.0, 'sex': '女'}
{'_id': 2.0, 'name': 'juhao222', 'age': 20.0}
{'_id': ObjectId('5ca379998a6e9e095b36641a'), 'name': 'juhao', 'age': 20}
{'_id': ObjectId('5ca37cde8a6e9e0967ae7f89'), 'name': 'juhao', 'age': 20}
{'_id': ObjectId('5ca37d1b8a6e9e09768d0348'), 'name': 'juhao', 'age': 20}

----------------------

#课堂练习xshell
mongo
show dbs
use tanzhou
db
db.createCollection('table')
show collections
db.table.insert({_id: 1, name: 'juhao', age:18})
db.table.insert([
{name:'juhao', sex:'男', age:18},
{name:'nanbei', sex:'男', age:18},
{name:'budong', sex:'男', age:18},
])
db.table.find()
db.table.find().pretty()
db.table.insert({_id: 2, name: 'juhao', age:22})
db.table.find( { $and:[{name:'juhao'},{age:{$gt:18}}] } )

db.table.update({sex:'男'},{age:20}) //全文档替换:把条件是sex=男的,age变成20
db.table.update({name:'juhao'}, {$set: {age:666, sex: 'xx'}} ) //指定属性更新: { $set: {age:20} }
db.table.update({sex:'男'}, {$set:{sex:'女'}}, { multi:true} ) //更新多条: { multi: ture }

猜你喜欢

转载自www.cnblogs.com/tiantiancode/p/12900315.html