爬虫--MongoDB安装及应用

官文敬上

python连接mongodb

#下载包
pip install pymango


from pymongo import MongoClient
#获取客户端
client = MongoClient() 
client = MongoClient("mongodb://mongodb0.example.net:27019")
client = MongoClient('localhost', 27017)

#连接数据库
db = client.test
db = clinet['test']

#获取集合
collection = db.col_name
collection = db['col_name']

#插入数据
collection.insert_one(doc)
collection.insert_many(docs)

#查询数据
find(filter=None, projection=None, skip=0, limit=0,
        no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE,
        sort=None, allow_partial_results=False, oplog_replay=False,
        modifiers=None, manipulate=True)
        
find_one(filter_or_id=None, *args, **kwargs) 

#遍历查询数据
for doc in collection.find()
	print(doc)

#条件查询
from bson import ObjectId
doc = collection.find({'_id':ObjectId('5cb82fab9510257d2ef26f3d')}) #使用_id查询

#更新文档
collection.update_one(filter, update, upsert=False)
collection.update_many(filter, update, upsert=False)
collection.replace_one(filter, replacement, upsert=False) 
collection.find_one_and_update(filter, update, projection=None, sort=None, return_document=ReturnDocument.BEFORE, **kwargs)


#删除文档
collection.delete_one(filter)
collection.delete_many(filter)
collection.drop() #删除集合
collection.find_one_and_delete(filter, projection=None, sort=None, **kwargs)
collection.find_one_and_replace(filter, replacement, projection=None, sort=None, return_document=ReturnDocument.BEFORE, **kwargs)

#索引
collection.create_index(keys, **kwargs)
collection.create_indexes(indexes)
collection.drop_index(index_or_name)
collection.drop_indexes()
collection.reindex()
collection.list_indexes()
collection.index_information()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

像风一样的男人@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值