mongoDB笔记(二)-在python中使用mongoDB

以下内容均基于WIN10环境

前提准备

要在python中调用mongoDB,首先需要对应的包pymongo,直接在pip或其他环境中下就好了

pip install pymongo

由于mongoDB在WIN10环境下自动启动,所以不需要专门打开。如果没有打开的话,或许可以任务管理器一波?

链接数据库

pymongo是面向对象方式组织的,和mongoDB一致,比较令人迷惑的一点是,mongoDB中的方法都是蛇形命名法,但pymongo都是小驼峰命名法。不过只要记住了这一点,转换一下方法名就可以用了。

import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# OO链接数据库
db = client["database_name"]
# 或 db = client.database_name 
# OO获取文档集
test = db["test"]
# 或 test = db.test

具体操作

后续操作直接对着文档集对象怼即可,其方法名如上所述,就是把mongoDB的方法改成蛇形命名法,具体参数和原方法一样,单个文档应该是对应python的dict,多个维度对应一个dictlist

需要注意的一点是,python的dict的键值必须用引号括起来,这一点和mongoDB略有区别,转换代码的时候要先预处理一下。

# 随便举个例子
student_coltu = db.student
# 查询语句,查找所有女生的名字
query = {"gender": "f"}
projection = {"_id": 0,"name":1}
female_name = student_col.find(query,projection)#(query, projection)

student_col.delete_many({})

student =[
   { "name": "Joe","gender": "m", "age": 23, "birthdate": { "day": 15, "month": 3, "year":
1997 }, "hobby": ["football", "basketball", "reading"], "city": "Beijing", "time": [9, 18] },
  { "name": "Kate",  "gender": "f", "age": 22, "birthdate": { "day": 25, "month": 7, "year": 1998
}, "hobby": ["reading", "piano"], "city": "Hangzhou", "time": [8, 17] },
  { "name": "Rose",  "gender": "f", "age": 24, "birthdate": { "day": 3, "month": 3, "year": 1996
}, "hobby": ["basketball", "running", "traveling"], "city": "Shanghai", "time": [9, 19] },
  { "name": "Jason",  "gender": "m", "age": 21, "birthdate": { "day": 17, "month": 12, "year":
1999 }, "hobby": ["cooking", "photography"], "city": "Chengdu", "time": [8, 20] },
  { "name": "Grace",  "gender": "f", "age": 22, "birthdate": { "day": 10, "month": 6, "year":
1998 }, "hobby": ["photography",  "cooking", "drama"], "city": "Nanjing", "time": [9, 18] },
  { "name": "Jessica",  "gender": "f", "age": 22, "birthdate": { "day": 21, "month": 3, "year":
1998 }, "hobby": ["cooking", "piano"], "city": "Shanghai", "time": [10, 19] }
]

student_col.insert_many(student)

另一个容易掉的坑是find()的返回对象,其类型是pymongo.cursor.Cursor,但这个对象不存在cursor对象的fetchall()/fetchone()方法(这似乎是对应mySQL的),而且也不能直接输出。但除了这一点以外,它的属性和list基本一致,支持切片(除了步长),for i in cursor这种形式的遍历,遍历得到的对象是普通的dict,所以为什么不能直接返回一个list呢?

posted @ 2020-09-27 19:43  wengsy150943  阅读(150)  评论(0编辑  收藏  举报