MongoDB mapReduce案例分析

文章目录


第1关:mongoDB的插入和查询

编程要求

根据提示,在右侧编辑器补充代码,根据提示。

测试说明

平台会对你编写的代码进行测试:

测试输入:

无输入;

预期输出:

输出查询匹配到的数据

c 复制代码
import pymongo

#连接mongodb
client= pymongo.MongoClient(
  host="127.0.0.1",
  port=27017
)
mydb = client["mydatabase"]
mycol = mydb["customers"]
mycol.drop()
mycol = mydb["customers"]

book1 = {
    '_id' : 0,
    'name': '五年高考三年模拟',
    'price': 50,
}

#***************Begin**************#
mycol.insert_one(book1)
#***************End****************#


book2= [
    {
    '_id' :  1,
    'name': '高考',
    'price': 50,
},

    {
    '_id' :  2,
    'name': '加油',
    'price': 50,
},
    {
    '_id'  : 3, 
    'name': '三年模拟',
    'price': 30,
},
]


#***************Begin**************#
mycol.insert_many(book2) #使用insert_many插入多个数据book2                                  
x = mycol.find_one({'price':50})#使用find_one查询第一个{'price':50}数据并打印出来
print(x)                        
x = mycol.find_one({'price':50})#使用find查询多个{'price':50}数据并打印出来
for i in mycol.find({'price':50}):
 print(i)
#***************End****************#

第2关:MongoDB的删除操作

编程要求

根据提示,在右侧编辑器补充代码,根据提示。

测试说明

平台会对你编写的代码进行测试:

测试输入:

无输入;

预期输出:

输出查询匹配到的数据

开始你的任务吧,祝你成功!

c 复制代码
import pymongo

#连接mongodb
client= pymongo.MongoClient(
  host="127.0.0.1",
  port=27017
)
mydb = client["mydatabase"]
mycol = mydb["customers"]
mycol.drop()
mycol = mydb["customers"]

book1 = {
    '_id' : 0,
    'name': '五年高考三年模拟',
    'price': 50,
}
result = mycol.insert_one(book1) 
book2= [
    {
    '_id' :  1,
    'name': '高考',
    'price': 50,
},

    {
    '_id' :  2,
    'name': '加油',
    'price': 50,
},
    {
    '_id'  : 3, 
    'name': '三年模拟',
    'price': 30,
},
]

resutl = mycol.insert_many(book2) 


print("删除前的数据")
#***************Begin**************# 
for x in mycol.find({'price':50}):
    print(x)                                        #用find查询打印出删除前含有{'price':50}的数据

mycol.delete_one({'price':50})                                        #使用delete_one删除一个数据{'price':50}
print("删除后的数据")
for x in mycol.find({'price':50}):
    print(x)                                         #用find查询打印出删除后含有{'price':50}的数据

#***************End****************#
相关推荐
t***442342 分钟前
mysql之数字函数
数据库·mysql
zlpzpl1 小时前
MySQL 的 INSERT(插入数据)详解
android·数据库·mysql
摩拜芯城IC1 小时前
8通道TDM分组传输IC DS34S108GN芯片参数资料 E1/T1分组化芯片
数据库
u***35741 小时前
MySQL ——数据的增删改查
数据库·mysql
zjttsh2 小时前
MySQL 数据库基础
数据库·mysql·oracle
万邦科技Lafite2 小时前
淘宝店铺所有商品API接口实战指南
java·数据库·mysql
mqffc3 小时前
Mysql 驱动程序
数据库·mysql
wl85113 小时前
SAP-CPI-SF问题收集009 user id is either invalid or purged
数据库
摩拜芯城IC4 小时前
RS -485/RS -422 全双工收发器 ISO3086TDW芯片参数资料 驱动集成 IC
数据库
e***13624 小时前
MySQL 常用 SQL 语句大全
数据库·sql·mysql