mongodb & pymongo

mongodb & pymongo

mongodb

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

集合:对应数据库表

文档:对应数据库一条数据

插入文档

save():如果 _id 主键存在则更新数据,如果不存在就插入数据。该方法新版本中已废弃,可以使用 db.collection.insertOne() 或 db.collection.replaceOne() 来代替。

insert(): 若插入的数据主键已经存在,则会抛 org.springframework.dao.DuplicateKeyException 异常,提示主键重复,不保存当前数据。

3.2 版本之后新增了 db.collection.insertOne() 和 db.collection.insertMany()

mongodb-insertOne

db.getCollection('test').insertOne({"price": 15, "number": 10, "name": "apple"})

mongodb-insertOne

db.getCollection('test').insertMany([{"price": 15, "number": 10, "name": "apple"}, {"price": 10, "number": 20, "name": "orange"}])

pymonodb-insert

import pymongo

host = "xxx"

port = 30019

db = pymongo.MongoClient(host=host, port=port)

# 集群

# pymongo.MongoClient("192.168.234.241:30019,192.168.234.241:30019")

# 获取所有数据库

db.database_names()

# test 数据库不存在则创建,有数据插入才会真实创建

db["test"]

# 获取数据库的集合

db["test"].collection_names()

# test 集合不存在则创建,有数据插入才会真实创建

document = db["test"]["test"]

# insert_one

db.get_database("jenkins").get_collection('test').insert_one({"price": 15, "number": 10, "name": "apple"})

# insert_many

db.get_database("jenkins").get_collection('test').insert_many([{"price": 5, "number": 10, "name": "watermelon"}, {"price": 10, "number": 30, "name": "grape"}])

# insert

db.get_database("jenkins").get_collection('test').insert([{"price": 5, "number": 11, "name": "plum"}])

db.get_database("jenkins").get_collection('test').insert({"price": 5, "number": 11, "name": "plum"})

db.get_database("jenkins").get_collection('test').insert([{"price": 5, "number": 10, "name": "pear"}, {"price": 20, "number": 20, "name": " strawberry "}])

# 集合更新文档

db.getCollection('job').update({"_id": "2021-11-33-15-46-31"}, {"status": "test"}, true)

# db.collection.update(criteria,objNew,upsert,multi)

# criteria 查询条件

# objNew 更新数据

# upsert 如果不存在update的记录,是否插入objNew这个新的document。true为插入,默认是false,不插入。

# multi 默认是false,只更新找到的第一条记录。如果为true,按条件查出来的多条记录全部更新。

查询

条件

{:{$lt:}} 小于{:{$gt:}} 大于{:{$gte:}} 大于等于{:{$lte:}} 小于等于{:{$ne:}} 不等于{$or: [{key1: value1}, {key2:value2}]} or{{key1: value1}, {key2:value2}} and{“key”: {“$in”: [value]}}{“key”: {“$nin”: [value]}}{“$regex”: “”} 正则表达{“$not”: “”}

mongodb-find

db.getCollection('test').find({"price": {"$gt": 10}})

db.getCollection('test').find({"price": {"$gt": 10}, "number": {"$lt": 20}})

db.getCollection('test').find({$or: [{"price": {"$gt": 5}}, {"number": {"$lt": 20}}]})

db.getCollection('test').find({"price": {"$in": [10, 15]}})

db.getCollection('test').find({"price": {"$nin": [10, 15]}})

db.getCollection('job').find({"projectBranch": {"$regex": "\\d{1}"}})

db.getCollection('job').find({"projectBranch": {$not: {"$regex": "\\d{1}"}}})

pymongo-find

# 大于

response = db.get_database("jenkins").get_collection('test').find({"price": {"$gt": 10}})

print(list(response))

# and

response = db.get_database("jenkins").get_collection('test').find({"price": {"$gt": 10}, "number": {"$lt": 20}})

print(list(response))

# or

response = db.get_database("jenkins").get_collection('test').find({"$or": [{"price": {"$gt": 5}}, {"number": {"$lt": 20}}]})

print(list(response))

# in

response = db.get_database("jenkins").get_collection('test').find({"price": {"$in": [10, 15]}})

print(list(response))

# nin

response = db.get_database("jenkins").get_collection('test').find({"price": {"$nin": [10, 15]}})

print(list(response))

# regex

response = db.get_database("jenkins").get_collection('job').find({"projectBranch": {"$regex": "\\d{1}"}})

print(list(response))

# not

response = db.get_database("jenkins").get_collection('job').find({"projectBranch": {"$not": {"$regex": "\\d{1}"}}})

print(list(response))

# 集合查询

collection.find({"_id": "2021-11-26-15-13-46"}, {"_id": 1})

# db.collection.find(query, projection)

# query 是可选参数,基于查询操作符指定了查询的条件

# projection 返回的字段,1为要返回,0位不返回,1和0不能同时使用

# collection.find({"_id": "2021-11-26-15-13-46"}, {"_id": 0, "userName": 0}) 正确

# collection.find({"_id": "2021-11-26-15-13-46"}, {"_id": 1, "userName": 0}) 错误

排序、skip、limit

# sort 1 为升序排列,而 -1 是用于降序排列。

# skip 跳过多少条

# limit 取多少条

db.getCollection('job').find({"projectBranch": {$not: {"$regex": "\\d{1}"}}}).sort({"projectBranch": 1}).skip(1).limit(2)

# 顺序无关

db.getCollection('job').find({"projectBranch": {$not: {"$regex": "\\d{1}"}}}).sort({"projectBranch": 1}).limit(2).skip(1)

聚合

表达式

$sum$max$min$avg$push 将值加入一个数组中,不会判断是否有重复的值$addToSet 将值加入一个数组中,判断是否有重复的值$ first 第一个$ last 最后一个

# 已libBranch 分组,统计个数

db.getCollection('job').aggregate([{$group : {_id : "$libBranch", num_tutorial : {$sum : 1}}}])

管道

$project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。match:用于过滤数据,只输出符合条件的文档。match:用于过滤数据,只输出符合条件的文档。match:用于过滤数据,只输出符合条件的文档。match使用MongoDB的标准查询操作。$limit:用来限制MongoDB聚合管道返回的文档数。$skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。$unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。$group:将集合中的文档分组,可用于统计结果。$sort:将输入文档排序后输出。$geoNear:输出接近某一地理位置的有序文档

mongodb-aggregate

db.getCollection('job').aggregate([{$match : {"projectBranch": {"$regex": "\\d{1}"}}}, {"$project": {"status": 1}}])

db.getCollection('job').aggregate([{$match : {"projectBranch": {"$regex": "\\d{1}"}}}, {"$project": {"status": 1}}, {"$skip": 1}, {"$limit": 2}])

# 顺序是有区别的

db.getCollection('job').aggregate([{$match : {"projectBranch": {"$regex": "\\d{1}"}}}, {"$project": {"status": 1}}, {"$limit": 2},{"$skip": 1}])

pymongo-aggregate

response = db.get_database("jenkins").get_collection('job').aggregate([{"$match" : {"projectBranch": {"$regex": "\\d{1}"}}}, {"$project": {"status": 1}}, {"$limit": 2},{"$skip": 1}])

print(list(response))

mongo+pandas

import pandas as pd

import pymongo

host = "xxx"

port = 30019

db = pymongo.MongoClient(host=host, port=port)

data = pd.DataFrame(db.get_database("jenkins").get_collection('test').find())

db.close()

相关推荐

龙虾的做法
365bet真人体育

龙虾的做法

🗓️ 08-30 👁️ 8945
网易有道怎么样?
最佳娱乐365bet娱乐场下载

网易有道怎么样?

🗓️ 07-04 👁️ 6202
cf穿越火线红点怎么开不了
365根据什么来封号

cf穿越火线红点怎么开不了

🗓️ 08-04 👁️ 1399
2011年世界女排大奖赛总决赛 中国队
365根据什么来封号

2011年世界女排大奖赛总决赛 中国队

🗓️ 06-30 👁️ 9087
二手华为手机价格表最新
365bet真人体育

二手华为手机价格表最新

🗓️ 07-23 👁️ 7924
描写霸王的成语(形容自己霸王的成语)(24个)
365bet真人体育

描写霸王的成语(形容自己霸王的成语)(24个)

🗓️ 10-05 👁️ 2880
蜻蜓白卡是什么 蜻蜓白卡审核要多久?
365根据什么来封号

蜻蜓白卡是什么 蜻蜓白卡审核要多久?

🗓️ 08-23 👁️ 2102
一文掌握 YUV 图像的基本处理
365根据什么来封号

一文掌握 YUV 图像的基本处理

🗓️ 09-13 👁️ 1854
手机QQ达人断了怎么办?
最佳娱乐365bet娱乐场下载

手机QQ达人断了怎么办?

🗓️ 09-25 👁️ 2478
鬼怕猫还是猫怕鬼
最佳娱乐365bet娱乐场下载

鬼怕猫还是猫怕鬼

🗓️ 07-23 👁️ 1360
哪些SUV符合国六排放标准
最佳娱乐365bet娱乐场下载

哪些SUV符合国六排放标准

🗓️ 07-05 👁️ 7478
如何拒绝微信好友的验证请求? ( 如何关闭微信好友验证功能? )