当前位置:Gxlcms > 数据库问题 > python操作mongodb

python操作mongodb

时间:2021-07-01 10:21:17 帮助过:3人阅读

import pymongo
client=pymongo.MongoClient("192.168.14.179",27017)
db=client[‘test‘]
stu=db[‘stu‘]
s1={‘name‘:‘lz‘,‘age‘:27}
s1_id=stu.insert_one(s1).inserted_id
s2=stu.find_one()
for i in stu.find():
print(i)
#方法2
x=stu.find()
x.next()
x.next()
x.next()
x.next()


print(stu.count())

#结果
E:\py1902project\venv\Scripts\python.exe E:/py1902project/xxx/xxx.py
{‘_id‘: ObjectId(‘5ca34a005daca34f28389b11‘), ‘name‘: ‘wang‘, ‘gender‘: 1.0}
{‘_id‘: ObjectId(‘5ca34a0a5daca34f28389b12‘), ‘name‘: ‘chen‘, ‘gender‘: 1.0}
{‘_id‘: ObjectId(‘5ca34a0e5daca34f28389b13‘), ‘name‘: ‘chen‘, ‘gender‘: 1.0}
{‘_id‘: ObjectId(‘5ca34a7a5daca34f28389b14‘), ‘name‘: ‘da‘}
{‘_id‘: ‘1002‘, ‘name‘: ‘wbk‘}
{‘_id‘: ObjectId(‘5ca35d16428ed81704b9f6b6‘), ‘name‘: ‘lz‘, ‘age‘: 27}
6

Process finished with exit code 0

python操作mongodb

标签:for   finish   end   exe   python   name   script   ted   操作   

人气教程排行