centos7看这里这个教程我实际操作,完全可以。
下面就是windows的了。
最近学习爬虫,需要用到MongoDB,特此记录最基础的数据库操作。
启动数据库
(1)安装好数据库后,打开cmd,进入数据库安装的bin目录:

(2)然后输入:mongod.exe --dbpath D:\MongoDB\data\db
mongod.exe表示启动数据库, --dbpath 后边是数据存档的路径,我自己建的db文件夹。也可以输入:
mongod.exe --dbpath D:\MongoDB\data\db
--logpath D:\MongoDB\logs\mongodb.log --logpath后边就是你的日志存放路径,一般我不用。

(3)等数据库出现等待链接端口27017(这是MongoDB默认端口号)的时候就显示启动正常

为了避免每次启动,可以直接设置为服务:设置momgo为linux和windows服务
连接数据库
连接数据库有三种常用的方式:
第一种:重新打开一个cmd(这是第二个cmd,之前的那个不要关),进入bin目录,输入:
mongo.exe

第一个cmd显示如下:

出现上面的画面表示连接成功,可以输入命令进行操作了(在d第二个cmd操作)

第二种 :利用MongoDB的可视化操作工具,这里是 MongoDB自带的

打开之后直接点击链接按钮:

数据库就可视化成功了

第三种 :利用python程序链接数据库,这也是我主要学习的,下面详细给出这种的示例。
首先需要导入python自带的数据库模块:(没安装的可以用 pip install pymongo安装)
import pymongo
链接数据库:
#port不输入默认就是27017,也可是直接输入IP'192.168.22.21'
client = pymongo.MongoClient(host = 'localhost',port = 27017)
#远程连接mongondb,密码登录
client = pymongo.MongoClient('mongodb://root:123456@localhost:27017/')
#或者传入参数,标准的字符串处理方式
client = pymongo.MongoClient('mongodb://{0}:{1}@{2}:27017/'.format(username,passwd,ip))
创建数据库:
#两种方式任选其一就可以
#第一种
db = client.example
#第二种
db = client['example']
创建collection,一个数据库可以包含很多collection
#两种方式任选其一就可以
#第一种
collection = db.student
#第二种
collection = db['student']
这里是数据:
student1 = {
'name' :'Bob',
'age' : '30',
'gender' :'male'
}
student2 = {
'name' :'Tom',
'age' : '30',
'gender' :'male'
}
student3 = {
'name' :'Lucy',
'age' : '19',
'gender' :'female'
}
插入数据:
#insert()已经被弃用,但是还可以用
#insert_one()就是插入一个
collection.insert_one(student1)
#insert_many()的参数是一个列表
collection.inesert_many([student2,student3])
查询数据:
#查询一个参数是字典,返回第一条符合的数据
result = collection.find_one({'name':'Bob'})
#查询多条,返回全部符合的数据,要遍历取出结果
results= collection.find({'age':'20'})
for result in results:
print (result)
条件查询,一般爬虫用来存数据多
#关于其他$gt之外用法,参考官网
#查询大于20岁的,也需要遍历取出结果,gt表示大于
results = collection.find('age':'{'$gt':'20'}')
for result in results:
print(result['name'],result['age'])
统计数据库:
#collection.find().count()过时了,统计总数用
count = collection.estimated_document_count()
#条件统计
count = collection.count_documents({'age':'20'})
排序:
#按照升序排序
results = collection.find().sort('age':pymongo.ASCENDING)
偏移量:
#skip(30)表示忽略前30个数据
result = collection.find().sort('age':pymongo.ASCENDING).skip(30)
限制输出:
#只输出前两个数据
result = results = collection.find().limit(2)
#也可以加入限制
results = collection.find().sort('age':pymongo.ASCENDING).limit(10)
更新数据:
#条件,关于其他用法,$inc等用法,参考官网
condition = {'age':'666'}
#更新一个数据$set表示重新设置此值
result = collection.update_one(condition,{'$set':{'age':222}})
#更新全部的符合条件的数据
result = collection.update_many(condition,{'$set':{'age':222}})
数据库插入前去重
即数据库有存在的数据,只进行更新
这里的news是一个字典,有很多字段,其中url和title只是其中一部分,设定url和title为过滤的字眼,当出现相同的时候,就只进行更新。如果没有的时候就进行插入。
collection.update({'url':news['url'],'title':news['title']},{'$set':news},upsert = True)
删除数据;
#删除一个
result = collection.delete_one({'name':'Bob'})
#删除很多
result = collection.delete_many({'age':'222'})
记录一个问题,是我安装时出错的:
mongodb使用mongo报错: Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by ::
解决方法:关掉cmd,重新启动,重新连接,按照我前边的方法做就好了
如果您有不同的想法,请您留言,让我们一起学习,一起进步。谢谢。
网友评论