服务器之家

服务器之家 > 正文

Python操作MongoDB数据库的方法示例

时间:2020-12-31 00:42     来源/作者:chengqiuming

本文实例讲述了Python操作MongoDB数据库的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
>>> import pymongo
>>> client=pymongo.MongoClient ('localhost',27017)
>>> db=client.students
>>> db.collection_names()
['students']
>>> students=db.students
>>> students.find()
<pymongo.cursor.Cursor object at 0x0000017A74305FD0>
>>> for item in students.find():
  print(item)
{'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'}
>>> wangwu={'name':'wangwu','age':20,'sex':'male'}
>>> students.insert(wangwu)
ObjectId('593a7c5fedb5a1abeb757052')
>>> for item in students.find({'name':'wangwu'}):
  print(item)
{'_id': ObjectId('593a7c5fedb5a1abeb757052'), 'name': 'wangwu', 'age': 20, 'sex': 'male'}
>>> students.find_one()
{'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'}
>>> students.find_one({'name':'wangwu'})
{'_id': ObjectId('593a7c5fedb5a1abeb757052'), 'name': 'wangwu', 'age': 20, 'sex': 'male'}
>>> students.find().count()
2
>>> students.remove({'name':'wangwu'})
{'ok': 1, 'n': 1}
>>> for item in students.find():
  print(item)
{'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'}
>>> students.find().count()
1
>>> students.create_index([('name',pymongo.ASCENDING)])
'name_1'
>>> students.update({'name':'zhangsan'},{'$set':{'age':25}})
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}
>>> students.find_one()
{'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 25, 'sex': 'male'}
>>> students.update({'age':25},{'$set':{'sex':'Female'}})
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}
>>> students.remove()
{'ok': 1, 'n': 1}
>>> students.find().count()
0
>>> zhangsan={'name':'zhangsan','age':25,'sex':'Male'}
>>> lisi={'name':'lisi','age':21,'sex':'Male'}
>>> wangwu={'name':'wangwu','age':22,'sex':'Female'}
>>> students.insert_many([zhangsan,lisi,wangwu])
<pymongo.results.InsertManyResult object at 0x0000017A749FC5E8>
>>> for item in students.find().sort('name',pymongo.ASCENDING):
  print(item)
{'_id': ObjectId('593a806bedb5a1abeb757054'), 'name': 'lisi', 'age': 21, 'sex': 'Male'}
{'_id': ObjectId('593a806bedb5a1abeb757055'), 'name': 'wangwu', 'age': 22, 'sex': 'Female'}
{'_id': ObjectId('593a806bedb5a1abeb757053'), 'name': 'zhangsan', 'age': 25, 'sex': 'Male'}
>>> for item in students.find().sort([('sex',pymongo.DESCENDING),('name',pymongo.ASCENDING)]):
  print(item)
{'_id': ObjectId('593a806bedb5a1abeb757054'), 'name': 'lisi', 'age': 21, 'sex': 'Male'}
{'_id': ObjectId('593a806bedb5a1abeb757053'), 'name': 'zhangsan', 'age': 25, 'sex': 'Male'}
{'_id': ObjectId('593a806bedb5a1abeb757055'), 'name': 'wangwu', 'age': 22, 'sex': 'Female'}
>>>

希望本文所述对大家Python程序设计有所帮助。

原文链接:http://blog.csdn.net/chengqiuming/article/details/78601256

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
返回顶部