从零开始:在flask中使用mongodb

先安装mongodb,再玩玩命令行

MongoDB 极简实践入门
可以用这种方式连接MongoDB:

mongo
或
mongo --host=127.0.0.1 --port=27017

可以输入exit或ctrl+c来退出连接
如果还遇到其他什么问题再来看这个MongoDB安装、启动与连接

Mongodb的python客户端

看官方文档学习很不错
注:pymongo和mongoengine从使用上来说,两者是独立的,使用pymongo就不用去建orm模型了,开发的时候两者选一个用就行了,不过听说mongoengine效率比较低

pymongo(类似于pymysql)

语法类似于mongo原生的语法
github - pymongo:https://github.com/mongodb/mongo-python-driver
pymongo文档:https://pymongo.readthedocs.io/
pymongo本身是线程安全的,但是进程不安全:https://pymongo.readthedocs.io/en/stable/faq.html#id1

mongoengine(类似于SQLAlchemy)

本身依赖pymongo可以按照类似关系型数据库来定义数据的结构,使得操作风格和关系型数据库相似,可以使得代码更好的维持MVC结构。
官网:http://mongoengine.org/
文档:http://docs.mongoengine.org/
github:https://github.com/MongoEngine/mongoengine

flask-pymongo

flask操作mongodb的插件,基于pymongo,用法跟pymongo基本一致
flask-pymongo - github:https://github.com/dcrosta/flask-pymongo
flask-pymongo文档:https://flask-pymongo.readthedocs.io/

Flask 扩展 Flask-PyMongo:https://www.cnblogs.com/Erick-L/p/7047064.html

flask-mongoengine

flask操作mongodb的插件,基于mongoengine
flask-mongoengine - github:https://github.com/MongoEngine/flask-mongoengine
flask-mongoengine文档:https://flask-mongoengine.readthedocs.io

好文帮助快速入门举例了解和一些问题解决:在Flask中使用MongoDB:Flask-MongoEngine
mongoengine使用笔记:https://www.linuxzen.com/mongoengineshi-yong-bi-ji.html
随便看看Flask-MongoEngine官方文档·中文版:https://www.jianshu.com/p/b929b3671240
flask mongoengine的使用:https://www.jianshu.com/p/fe6727f549d2
Flask框架——MongoEngine使用MongoDB数据库:https://blog.csdn.net/weixin_52122271/article/details/126214545

参考资料

MongoDB官网:https://www.mongodb.com/
MongoDB中文站(感觉东西不是特别全,不如菜鸟和w3cschool):https://www.mongodb.org.cn/
MongoDB学习笔记(六)——MongoDB配置用户账号与访问控制:https://blog.csdn.net/qq_33206732/article/details/79877948
MongoDB安装及设置服务启动:https://blog.csdn.net/dandanfengyun/article/details/95497728
MongoDB之python简单交互(三) - pymongo, flask-pymongo, flask-mongoengine三者使用对比简介:https://www.cnblogs.com/cwp-bg/p/9473144.html

补充

pymongo 常见用法(了解查询,不需要硬记)

mongo –path

db.AddUser(username,password) 添加用户

db.auth(usrename,password) 设置数据库连接验证

db.cloneDataBase(fromhost) 从目标服务器克隆一个数据库

db.commandHelp(name) returns the help for the command

db.copyDatabase(fromdb,todb,fromhost) 复制数据库fromdb—源数据库名称,todb—目标数据库名称,fromhost—源数据库服务器地址

db.createCollection(name,{size:3333,capped:333,max:88888}) 创建一个数据集,相当于一个表

db.currentOp() 取消当前库的当前操作

db.dropDataBase() 删除当前数据库

db.eval(func,args) run code server-side

db.getCollection(cname) 取得一个数据集合,同用法:db['cname'] or db.cname

db.getCollectionNames() 取得所有数据集合的名称列表

db.getLastError() 返回最后一个错误的提示消息

db.getLastErrorObj() 返回最后一个错误的对象

db.getMongo() 取得当前服务器的连接对象get the server connection object

db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair

db.getName() 返回当操作数据库的名称

db.getPrevError() 返回上一个错误对象

db.getProfilingLevel() ?什么等级

db.getReplicationInfo() ?什么信息

db.getSisterDB(name) get the db at the same server as this onew

db.killOp() 停止(杀死)在当前库的当前操作

db.printCollectionStats() 返回当前库的数据集状态

db.printReplicationInfo()

db.printSlaveReplicationInfo()

db.printShardingStatus() 返回当前数据库是否为共享数据库

db.removeUser(username) 删除用户

db.repairDatabase() 修复当前数据库

db.resetError()

db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}

db.setProfilingLevel(level) 0=off,1=slow,2=all

db.shutdownServer() 关闭当前服务程序

db.version() 返回当前程序的版本信息

db.linlin.find({id:10}) 返回linlin数据集ID=10的数据集

db.linlin.find().count() 返回linlin数据集数据总数

db.linlin.find().limit(2) limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数

db.linlin.find().skip(8) skip方法同样接受一个数字参数作为跳过的记录条数

db.linlin.find({id:10}).limit(2).skip(8) 返回linlin数据集ID=1=的数据集从第二条到第八条的数据

db.linlin.find({id:10}).sort() 返回linlin数据集ID=10的排序数据集

db.linlin.findOne([query]) 返回符合条件的一条数据

db.linlin.getDB() 返回此数据集所属的数据库名称

db.linlin.getIndexes() 返回些数据集的索引信息

db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})

db.linlin.mapReduce(mayFunction,reduceFunction,)

db.linlin.remove(query) 在数据集中删除一条数据

db.linlin.renameCollection(newName) 重命名些数据集名称

db.linlin.save(obj) 往数据集中插入一条数据

db.linlin.stats() 返回此数据集的状态

db.linlin.storageSize() 返回此数据集的存储大小

db.linlin.totalIndexSize() 返回此数据集的索引文件大小

db.linlin.totalSize() 返回些数据集的总大小

db.linlin.update(query,object[,upsert_bool])在此数据集中更新一条数据

db.linlin.validate() 验证此数据集

db.linlin.getShardVersion() 返回数据集共享版本号

db.linlin.find({‘name’:'foobar’}) select * from linlin where name=’foobar’

db.linlin.find() select * from linlin

db.linlin.find({‘ID’:10}).count() select count(*) from linlin where ID=10

db.linlin.find().skip(10).limit(20) 从查询结果的第十条开始读20条数据 select * from linlin limit 10,20 ———-mysql

db.linlin.find({‘ID’:{$in:[25,35,45]}}) select * from linlin where ID in (25,35,45)

db.linlin.find().sort('id',-1) select * from linlin order by ID desc

db.linlin.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name) from linlin where ID<20

db.linlin.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})select name,sum(marks) from linlin group by name

db.linlin.find('this.ID<20′,{name:1}) select name from linlin where ID<20

db.linlin.insert({'name':'foobar’,'age':25}) insert into linlin ('name','age’)values('foobar',25)

db.linlin.insert({'name':'foobar’,'age':25,’email’:'cclove2@163.com’})

db.linlin.remove({}) delete * from linlin

db.linlin.remove({'age':20}) delete linlin where age=20

db.linlin.remove({'age':{$lt:20}}) delete linlin where age<20

db.linlin.remove({'age':{$lte:20}}) delete linlin where age<=20

db.linlin.remove({'age':{$gt:20}}) delete linlin where age>20

db.linlin.remove({‘age’:{$gte:20}}) delete linlin where age>=20

db.linlin.remove({‘age’:{$ne:20}}) delete linlin where age!=20

db.linlin.update({‘name’:'foobar’},{‘$set’:{‘age’:36}}) update linlin set age=36 where name=’foobar’

db.linlin.update({‘name’:'foobar’},{$inc’:{‘age’:3}}) update linlin set age=age+3 where name=’foobar’
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值