python操作mongodb
Posted ldsice
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python操作mongodb相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章