您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python中操作MongoDB

bubuko 2022/1/25 19:26:34 python 字数 1709 阅读 842 来源 http://www.bubuko.com/infolist-5-1.html

在python中操作MongoDB pip3 install pymongo python3 >>> from pymongo import MongoClient >>> client = MongoClient('mongodb://127.0.0.1:27017') >>> db = clie ...

在python中操作MongoDB

pip3 install pymongo
python3
>>> from pymongo import MongoClient
>>> client = MongoClient(‘mongodb://127.0.0.1:27017‘)
>>> db = client.school
>>> for student in db.students.find():
...     print(‘学号:‘, student[‘stuid‘])
...     print(‘姓名:‘, student[‘name‘])
...     print(‘电话:‘, student[‘tel‘])
...
学号: 1001.0
姓名: 张三
电话: 13566778899
学号: 1002.0
姓名: 王大锤
电话: 13012345678
学号: 1003.0
姓名: 白元芳
电话: 13022223333
>>> db.students.find().count()
3
>>> db.students.remove()
{‘n‘: 3, ‘ok‘: 1.0}
>>> db.students.find().count()
0
>>> coll = db.students
>>> from pymongo import ASCENDING
>>> coll.create_index([(‘name‘, ASCENDING)], unique=True)
‘name_1‘
>>> coll.insert_one({‘stuid‘: int(1001), ‘name‘: ‘张三‘, ‘gender‘: True})
<pymongo.results.InsertOneResult object at 0x1050cc6c8>
>>> coll.insert_many([{‘stuid‘: int(1002), ‘name‘: ‘王大锤‘, ‘gender‘: False}, {‘stuid‘: int(1003), ‘name‘: ‘白元芳‘, ‘gender‘: True}])
<pymongo.results.InsertManyResult object at 0x1050cc8c8>
>>> for student in coll.find({‘gender‘: True}):
...     print(‘学号:‘, student[‘stuid‘])
...     print(‘姓名:‘, student[‘name‘])
...     print(‘性别:‘, ‘男‘ if student[‘gender‘] else ‘女‘)
...
学号: 1001
姓名: 张三
性别: 男
学号: 1003
姓名: 白元芳
性别: 男
>>>

python中操作MongoDB

原文:https://www.cnblogs.com/zeeyan/p/13690618.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶