当前位置: 移动技术网 > IT编程>数据库>MongoDB > Python操作MongoDB文档数据库

Python操作MongoDB文档数据库

2019年09月11日  | 移动技术网IT编程  | 我要评论

大光明电影院,慕君依依,3dmax2010下载

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实例
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",)
# 获取db
test_db = connect['test']
# 获取collection
collection = test_db['students']
# 构建document
document = {"author": "mike",  "text": "my first blog post!", "tags": ["mongodb", "python", "pymongo"], "date": datetime.now()}
# 插入document
one_insert = collection.insert_one(document=document)
print(one_insert.inserted_id)

# 通过条件过滤出一条document
one_result = collection.find_one({"author": "mike"})
# 解析document字段
print(one_result, type(one_result))
print(one_result['_id'])
print(one_result['author'])

注意:如果需要通过id查询一行document,需要将id包装为objectid类的实例对象
from bson.objectid import objectid
collection.find_one({'_id': objectid('5c2b18dedea5818bbd73b94c')})
  • 插入多行documents, 查询多行document, 查看collections有多少行document
from pymongo import mongoclient
from datetime import datetime
connect = mongoclient(host='localhost', port=27017, username="root", password="123456",)

# 获取db
test_db = connect['test']

# 获取collection
collection = test_db['students']
documents = [{"author": "mike","text": "another post!","tags": ["bulk", "insert"], "date": datetime(2009, 11, 12, 11, 14)},
{"author": "eliot", "title": "mongodb is fun", "text": "and pretty easy too!", "date": datetime(2009, 11, 10, 10, 45)}]
collection.insert_many(documents=documents)

# 通过条件过滤出多条document
documents = collection.find({"author": "mike"})

# 解析document字段
print(documents, type(documents))
print('*'*300)
for document in documents:
    print(document)
print('*'*300)
result = collection.count_documents({'author': 'mike'})
print(result)
  • 范围比较查询
from pymongo import mongoclient
from datetime import datetime

connect = mongoclient(host='localhost', port=27017, username="root", password="123456",)

# 获取db
test_db = connect['test']

# 获取collection
collection = test_db['students']

# 通过条件过滤时间小于datetime(2019, 1,1,15,40,3) 的document
documents = collection.find({"date": {"$lt": datetime(2019, 1,1,15,40,3)}}).sort('date')

# 解析document字段
print(documents, type(documents))
print('*'*300)
for document in documents:
    print(document)
  • 创建索引
from pymongo import mongoclient
import pymongo
from datetime import datetime

connect = mongoclient(host='localhost', port=27017, username="root", password="123456",)
# 获取db
test_db = connect['test']
# 获取collection
collection = test_db['students']
# 创建字段索引
collection.create_index(keys=[("name", pymongo.descending)], unique=true)
# 查询索引
result = sorted(list(collection.index_information()))
print(result)
  • document修改
from pymongo import mongoclient
connect = mongoclient(host='localhost', port=27017, username="root", password="123456",)

# 获取db
test_db = connect['test']

# 获取collection
collection = test_db['students']
result = collection.update({'name': 'robby'}, {'$set': {"name": "petter"}})
print(result)
注意:还有update_many()方法
  • document删除
from pymongo import mongoclient
connect = mongoclient(host='localhost', port=27017, username="root", password="123456",)

# 获取db
test_db = connect['test']

# 获取collection
collection = test_db['students']
result = collection.delete_one({'name': 'petter'})
print(result.deleted_count)
注意:还有delete_many()方法

4.mongodb odm 详解

  • mongodb odm 与 django orm使用方法类似;
  • mongoengine是一个对象文档映射器,用python编写,用于处理mongodb;
  • mongoengine提供的抽象是基于类的,创建的所有模型都是类;
# 安装mongoengine
pip install mongoengine
  • 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

# 嵌套文档
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')

    first_document = students.objects.first()

    all_document = students.objects.all()

    # 如果只有一条,也可以使用get
    specific_document = students.objects.filter(name='robby3')

    print(first_document.name, first_document.age, first_document.time)

    for document in all_document:
        print(document.name)

    for document in specific_document:
        print(document.name, document.age)

修改、更新、删除数据

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():使用给定的替换值更新嵌入的文档;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网