hua_mongodb操作命令

启动/关闭mongodb

mongod --config /home/hua/mongodb/etc/mongodb.conf
mongod --config /home/hua/mongodb/etc/mongodb.conf -shutdown  

删除数据库

# 查看所有数据库
show dbs
#切换到testdb下
use testdb 
#删除数据库
db.dropDatabase()

删除集合

db.collection.drop

权限登录验证:

db.auth("username","password")

查看集合:

show collections

查看集合信息

db.集合名.stats

cursor超时

1.修改MongoDB的配置,延长游标超时时间,并重启MongoDB。由于生产环境的MongoDB不能随便重启,所以这个方案虽然有用,但是排除。
2.一次性把数据全部读取下来,再做处理:

all_data = [row for row in handler.find()]
for row in all_data:
    parse(row)

3.每次连接数据库,只返回50行数据

for row in handler.find().batch_size(50): 
    parse_data(row)

4.让游标永不超时。通过设定参数no_cursor_timeout=True,让游标永不超时

cursor = handler.find(no_cursor_timeout=True)
try:
 for row in cursor:
 parse_data(row)
except Exception:
 parse_exception()
finally:
 cursor.close() # 一定要手动关闭游标

猜你喜欢

转载自blog.csdn.net/weixin_45041997/article/details/105946903