使用PyMongo查询MongoDB数据库
Posted Python之刃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PyMongo查询MongoDB数据库相关的知识,希望对你有一定的参考价值。
-
本文我们介绍如何使用PyMongo库查询MongoDB数据库。 -
本文我们介绍MongoDB中的基本聚合操作。
介绍
目录
-
什么是PyMongo? -
安装步骤 -
将数据插入数据库 -
查询数据库 -
根据字段进行过滤 -
根据比较运算符进行过滤 -
基于逻辑运算符进行过滤 -
常用表达 -
聚合管道 -
尾注
什么是PyMongo?
-
https://courses.analyticsvidhya.com/courses/introduction-to-data-science
安装步骤
pip3 install pymongo
将数据插入数据库
-
导入库并连接到mongo客户端
# 导入所需的库
import pymongo
import pprint
import json
import warnings
warnings.filterwarnings('ignore')
# 连接到mongoclient
client = pymongo.MongoClient('mongodb://localhost:27017')
# 获取数据库
database = client['sample_db']
-
从JSON文件创建集合
-
https://drive.google.com/drive/folders/1XqnNv5msuluuX5JV50sGYC0Uvwa7w2C2?usp=sharing
-
weekly_demand: -
id:每个文档的唯一ID -
week:周号 -
center_id:配送中心的唯一ID -
meal_id:餐的唯一ID -
checkout_price:最终价格,包括折扣,税金和送货费 -
base_price:餐的基本价格 -
emailer_for_promotion:发送电子邮件以促进进餐 -
homepage_featured:首页提供的餐点 -
num_orders:(目标)订单数 -
meal_info: -
meal_id:餐的唯一ID -
category:餐食类型(饮料/小吃/汤……) -
cuisine:美食(印度/意大利/…)
# 创建每周需求收集
database.create_collection("weekly_demand")
# 创建餐食信息
database.create_collection("meal_info")
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/925bfef1de8d47f4bfc667ddeedb6958.jpg)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/a1eb6c4a06254076bfdaeee3eace31e9.jpg)
-
将数据插入集合
# 获取collection weekly_demand
weekly_demand_collection = database.get_collection("weekly_demand")
# 打开weekly_demand json文件
with open("weekly_demand.json") as f:
file_data = json.load(f)
# 将数据插入集合
weekly_demand_collection.insert_many(file_data)
# 获取数据的总数量
weekly_demand_collection.find().count()
# >> 456548
# 获取收藏餐
meal_info_collection = database.get_collection("meal_info")
# 打开meat_info json文件
with open("meal_info.json") as f:
file_data = json.load(f)
# 将数据插入集合
meal_info_collection.insert_many(file_data)
# 获取数据的总数量
meal_info_collection.find().count()
# >> 51
weekly_demand_collection.find_one()
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/a4ed7c51f45746759cf2d0fa6ef35ae6.jpg)
meal_info_collection.find_one()
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/0e693cc5d5ae49c2967c79bd59b570fa.jpg)
查询数据库
your_collection.find( {<< query >>} , { << fields>>} )
weekly_demand_collection.find_one( {}, { "week": 1, "checkout_price" : 1})
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/afa8125f8f9a48699c66752f7d94fc5a.jpg)
weekly_demand_collection.find_one( {}, {"num_orders" : 0, "meal_id" : 0})
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/ca83d7e5c0124b0b8b06fbaec64ce0ce.jpg)
weekly_demand_collection.find_one( {"center_id" : 55, "meal_id" : 1885}, {"_id" : 0, "week" : 0} )
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/3b284a1360e945878c990b4c3c8dc07b.jpg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result_1 = weekly_demand_collection.find({
"center_id" : { "$eq" : 55},
"homepage_featured" : { "$ne" : 0}
})
for i in result_1:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/71bfdb77594b4d989706b39001973538.jpg)
result_2 = weekly_demand_collection.find({
"center_id" : { "$in" : [ 24, 11] }
})
for i in result_2:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/b08c7c067ad549acbe5ec81b7fdc596d.jpg)
result_3 = weekly_demand_collection.find({
"center_id" : { "$nin" : [ 24, 11] }
})
for i in result_3:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/f86b9b137f3f40869fb30f864aae5c37.jpg)
result_4 = weekly_demand_collection.find({
"center_id" : 55,
"checkout_price" : { "$lt" : 200, "$gt" : 100}
})
for i in result_4:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/533d84e3c3cd444c97f1cd52d21d0fee.jpg)
|
|
|
|
|
|
|
|
|
|
result_5 = weekly_demand_collection.find({
"$and" : [{
"center_id" : { "$eq" : 11}
},
{
"meal_id" : { "$ne" : 1778}
}]
})
for i in result_5:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/703e5207dd2246f79fee1261302c4bf2.jpg)
result_6 = weekly_demand_collection.find({
"$or" : [{
"center_id" : { "$eq" : 11}
},
{
"meal_id" : { "$in" : [1207, 2707]}
}]
})
for i in result_6:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/3b0ef11e19f5498dbdbd0f09cfd0ce75.jpg)
-
https://www.analyticsvidhya.com/blog/2015/06/regular-expression-python
result_7 = meal_info_collection.find({
"cuisine" : { "$regex" : "^C" }
})
for i in result_7:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/b8301be1bd07443384aa9165062c9cd2.jpg)
result_8 = meal_info_collection.find({
"$and" : [
{
"category" : {
"$regex" : "^S"
}},
{
"cuisine" : {
"$regex" : "ian$"
}}
]
})
for i in result_8:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/a28a77e61ab446a7a5382226de639927.jpg)
your_collection.aggregate( [ { <stage1> }, { <stage2> },.. ] )
result_9 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" :
{"center_id" : {"$eq" : 11 } }
},
## stage 2
{
"$count" : "total_rows"
}
])
for i in result_9:
print(i)
![使用PyMongo查询MongoDB数据库](https://image.cha138.com/20210331/d3a6e5f983f74dd9bc8fc086793d8796.jpg)
result_10 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" :
{"center_id" : {"$eq" : 11 } }
},
## stage 2
{
"$group" : { "_id" : 0 ,
"average_num_orders": { "$avg" : "$num_orders"},
"unique_meal_id" : {"$addToSet" : "$meal_id"}}
}
])
for i in result_10:
print(i)
尾注
-
https://courses.analyticsvidhya.com/courses/structured-query-language-sql-for-data-science
以上是关于使用PyMongo查询MongoDB数据库的主要内容,如果未能解决你的问题,请参考以下文章
遇到问题--mongodb---python---pymongo通过_id查询不到数据
遇到问题--mongodb---python---pymongo通过_id查询不到数据