1.Pymongo 安装
安装pymongo:
pip install pymongo
- PyMongo是驱动程序,使python程序能够使用Mongodb数据库,使用python编写而成;
2.Pymongo 方法
insert_one()
:插入一条记录;
insert()
:插入多条记录;
find_one()
:查询一条记录,不带任何参数返回第一条记录,带参数则按条件查找返回;
find()
:查询多条记录,不带参数返回所有记录,带参数按条件查找返回;
count()
:查看记录总数;
create_index()
:创建索引;
update_one()
:更新匹配到的第一条数据;
update()
:更新匹配到的所有数据;
remove()
:删除记录,不带参表示删除全部记录,带参则表示按条件删除;
delete_one()
:删除单条记录;
delete_many()
:删除多条记录;
3.Pymongo 中的操作
from pymongo import MongoClient
connect = MongoClient(host='localhost', port=27017, username="root", password="123456")
connect = MongoClient('mongodb://localhost:27017/', username="root", password="123456")
print(connect.list_database_names())
test_db = connect['test']
collection = test_db['students']
- 插入一行document, 查询一行document,取出一行document的值
from pymongo import MongoClient
from datetime import datetime
connect = MongoClient(host='localhost', port=27017, username="root", password="123456",)
- 插入多行documents, 查询多行document, 查看collections有多少行document
from pymongo import MongoClient
from datetime import datetime
connect = MongoClient(host='localhost', port=27017, username="root", password="123456",)
from pymongo import MongoClient
from datetime import datetime
connect = MongoClient(host='localhost', port=27017, username="root", password="123456",)
from pymongo import MongoClient
import pymongo
from datetime import datetime
connect = MongoClient(host='localhost', port=27017, username="root", password="123456",)
from pymongo import MongoClient
connect = MongoClient(host='localhost', port=27017, username="root", password="123456",)
from pymongo import MongoClient
connect = MongoClient(host='localhost', port=27017, username="root", password="123456",)
4.MongoDB ODM 详解
- MongoDB ODM 与 Django ORM使用方法类似;
- MongoEngine是一个对象文档映射器,用Python编写,用于处理MongoDB;
- MongoEngine提供的抽象是基于类的,创建的所有模型都是类;
BinaryField
BooleanField
ComplexDateTimeField
DateTimeField
DecimalField
DictField
DynamicField
EmailField
EmbeddedDocumentField
EmbeddedDocumentListField
FileField
FloatField
GenericEmbeddedDocumentField
GenericReferenceField
GenericLazyReferenceField
GeoPointField
ImageField
IntField
ListField:可以将自定义的文档类型嵌套
MapField
ObjectIdField
ReferenceField
LazyReferenceField
SequenceField
SortedListField
StringField
URLField
UUIDField
PointField
LineStringField
PolygonField
MultiPointField
MultiLineStringField
MultiPolygonField
5.使用mongoengine创建数据库连接
from mongoengine import connect
conn = connect(db='test', host='localhost', port=27017, username='root', password='123456', authentication_source='admin')
print(conn)
connect(db = None,alias ='default',** kwargs );
db
:要使用的数据库的名称,以便与connect兼容;
host
:要连接的mongod实例的主机名;
port
:运行mongod实例的端口;
username
:用于进行身份验证的用户名;
password
:用于进行身份验证的密码;
authentication_source
:要进行身份验证的数据库;
构建文档模型,插入数据
from mongoengine import connect, Document, StringField, IntField, FloatField, ListField, EmbeddedDocumentField, DateTimeField, EmbeddedDocument
from datetime import datetime
# 嵌套文档
class Score(EmbeddedDocument):
name = StringField(max_length=50, required=True)
value = FloatField(required=True)
class Students(Document):
choice = (('F', 'female'),
('M', 'male'),)
name = StringField(max_length=100, required=True, unique=True)
age = IntField(required=True)
hobby = StringField(max_length=100, required=True, )
gender = StringField(choices=choice, required=True)
# 这里使用到了嵌套文档,这个列表中的每一个元素都是一个字典,因此使用嵌套类型的字段
score = ListField(EmbeddedDocumentField(Score))
time = DateTimeField(default=datetime.now())
if __name__ == '__main__':
connect(db='test', host='localhost', port=27017, username='root', password='123456', authentication_source='admin')
math_score = Score(name='math', value=94)
chinese_score = Score(name='chinese', value=100)
python_score = Score(name='python', value=99)
for i in range(10):
students = Students(name='robby{}'.format(i), age=int('{}'.format(i)), hobby='read', gender='M', score=[math_score, chinese_score, python_score])
students.save()
查询数据
from mongoengine import connect, Document, StringField, IntField, FloatField, ListField, EmbeddedDocumentField, DateTimeField, EmbeddedDocument
from datetime import datetime
修改、更新、删除数据
from mongoengine import connect, Document, StringField, IntField, FloatField, ListField, EmbeddedDocumentField, DateTimeField, EmbeddedDocument
from datetime import datetime
# 嵌套文档
class Score(EmbeddedDocument):
name = StringField(max_length=50, required=True)
value = FloatField(required=True)
class Students(Document):
choice = (('F', 'female'),
('M', 'male'),)
name = StringField(max_length=100, required=True, unique=True)
age = IntField(required=True)
hobby = StringField(max_length=100, required=True, )
gender = StringField(choices=choice, required=True)
# 这里使用到了嵌套文档,这个列表中的每一个元素都是一个字典,因此使用嵌套类型的字段
score = ListField(EmbeddedDocumentField(Score))
time = DateTimeField(default=datetime.now())
if __name__ == '__main__':
connect(db='test', host='localhost', port=27017, username='root', password='123456', authentication_source='admin')
specific_document = Students.objects.filter(name='robby3')
specific_document.update(set__age=100)
specific_document.update_one(set__age=100)
for document in specific_document:
document.name = 'ROBBY100'
document.save()
for document in specific_document:
document.delete()
all()
:返回所有文档;
all_fields()
:包括所有字段;
as_pymongo()
:返回的不是Document实例 而是pymongo值;
average()
:平均值超过指定字段的值;
batch_size()
:限制单个批次中返回的文档数量;
clone()
:创建当前查询集的副本;
comment()
:在查询中添加注释;
count()
:计算查询中的选定元素;
create()
:创建新对象,返回保存的对象实例;
delete()
:删除查询匹配的文档;
distinct()
:返回给定字段的不同值列表;
嵌入式文档查询的方法
count()
:列表中嵌入文档的数量,列表的长度;
create()
:创建新的嵌入式文档并将其保存到数据库中;
delete()
:从数据库中删除嵌入的文档;
exclude(** kwargs )
:通过使用给定的关键字参数排除嵌入的文档来过滤列表;
first()
:返回列表中的第一个嵌入文档;
get()
:检索由给定关键字参数确定的嵌入文档;
save()
:保存祖先文档;
update()
:使用给定的替换值更新嵌入的文档;