python 操作 mongodb 数据库详情
作者:autofelix 发布时间:2024-01-19 17:53:45
标签:python,操作,mongodb
一、安装
pip install pymongo
二、连接数据库
import pymongo
# 方式一
client = pymongo.MongoClient('mongodb://localhost:27017')
# 方式二
client = pymongo.MongoClient('localhost',27017)
# 方式三,有密码认证
client = pymongo.MongoClient('localhost', 27017, username='xxx', password='xxx')
三、创建数据库
import pymongo
# 连接
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test # 或者 db = client['test']
print(db)
四、所有数据库
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
dbs = client.list_database_names()
五、创建集合
也就是数据库中的表
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user # 或者 collections = db['user']
# 删除表
collections.drop()
六、插入数据
insert_one:插入一条数据
insert_many:插入多条数据
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user
# 创建文档数据
user1 = {
'name': 'autofelix',
'age': '25',
'height': '172',
'weight': '60'
}
user2 = {
'name': '飞兔小哥',
'age': '28',
'height': '182',
'weight': '70'
}
# 插入一条文档集合
result = collections.insert_one(user1)
print(result)
print(result.inserted_id)
# 插入多条文档集合
result = collections.insert_many([user1, user2])
print(result)
print(result.inserted_ids)
七、查询数据
find:查询多条数据
find_one:查询一条数据
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user
# 查询所有
collections.find()
# 查询最近一条
collections.find_one()
# 根据条件查询
collections.find_one({'age':25})
八、高级查询
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user
# 跳过第一条查到的数据
collections.find({'age':{'$gt':10}},['height','age']).skip(1)
# limit限制查询条数
collections.find({'age':{'$gt':10}},['height','age']).limit(1)
# 多条件查询
collections.find_one({'height':{'$gt':150},'age':{'$lt':26,'$gt':10}})
# in查询,查询年龄在25,26,32的数据
collections.find({'age':{'$in':[25, 26, 32]}})
# or查询,查询年龄小于等于23或者大于等于29的数据
collections.find({'$or':[{'age':{'$lte':23}}, {'age':{'$gte':29}}]})
# exists查询
collections.find({'age':{'$exists':True}})
# 正则查询
collections.find({'name':{'$regex':r'.*auto.*'}})
九、count统计
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user
# 统计集合中总共有多少条数据
collections.find().count()
# 统计集合中年龄大于10岁的共有多少条数据
collections.find({'age':{'$gt':10}}).count()
十、修改数据
update_one:修改一条数据
update_many:修改多条数据
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user
# 修改一条数据
collections.update_one({'name': 'autofelix'}, {'$set': {'name': '大神'}})
# 修改多条数据
collections.update_many({'name': 'autofelix'}, {'$set': {'name': '大神'}})
十一、删除数据
delete_one:删除一条数据
delete_many:删除多条数据
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user
# 删除一条数据
collections.delete_one({'name': 'autofelix'})
# 删除多条数据
collections.delete_many({'name': 'autofelix'})
# 删除所有数据
collections.delete_many({})
十二、数据排序
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user
# 对字段 age 按升序排序
collections.find().sort('age')
# 对字段 age 按降序排序
collections.find().sort('age', -1)
# 多字段排序
collections.find().sort((('age',pymongo.ASCENDING),('height',pymongo.ASCENDING)))
来源:https://blog.51cto.com/autofelix/5195732


猜你喜欢
- pop()函数1、描述pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。语法pop()方法语法:list.p
- Http定义了与 服务器的交互方法,其中除了一般我们用的最多的GET,POST 其实还有PUT和DELETE 根据RFC2616标
- # -*- coding: cp936 -*-from scapy.all import *from threading import Th
- 前言在之前介绍fixture的文章中,我们使用到了conftest.py文件,那么conftest.py文件到底该如何使用呢,下面我们就来详
- 为了更直观的了解prometheus如何工作,本文使用prometheus的python库来做一些相应的测试。python库的github地
- 禁止鼠标右键:$(document).ready(function(){ $(document).bind("contextmen
- 将电话簿TeleAddressBook.txt和电子邮件EmailAddressBook.txt合并为一个完整的AddressBook.tx
- 一、如果models.py文件为时:timestamp = models.DateTimeField('保存日期')会提示:
- 在没学习开窗函数之前,我们都知道,用了分组之后,查询字段就只能是分组字段和聚合的字段,这带来了极大的不方便,有时我们查询时需要分
- 最近在学习关于Python数据分析与挖掘方面的知识,在学习到Python数据分析工具方面时,需要安装一些第三方扩展库来增强Python的数据
- 使用tkinter实现下拉多选框效果如图:1、选择一些选项2、全选选项代码如下:import tkinterfrom ComBoPicker
- 框架介绍在之前的.NET中,微软还没有提供过像样的日志框架,目前能用的一些框架比如Log4Net、NLog、CommonLogging使用起
- 目前搜索到的方法有:np.where(‘元素')还有就是pandas的方法:df.index(‘元素')但是第二个方法的问题
- 问题:根据数据某列进行分组,选择其中另一列大小top-K的的所在行数据解析:求解思路很清晰,即先用groupby对数据进行分组,然后再根据分
- /* 建立数据表 */ create table td_base_data( id int(10) not null auto_increm
- 今天我在练习python时,对字典里的键用sorted排序时发现并没有按照预期排序研究后发现字母大小写会影响排序首先创建一个字典,键里面的首
- Mysql Explain 详解一.语法explain < table_name >例如: explain select * f
- 本文实例为大家分享了js浏览器倒计时跳转页面效果,供大家参考,具体内容如下效果图:<!DOCTYPE html><html
- 连接配置方式如图:有时候Navicat并没有初始化安装sqlncli, 所以连接的时候会报 没有默认驱动,如图:解决方法:在navicat目
- 1、SQL SERVER 2005的性能工具中有SQL Server Profiler和数据库引擎优化顾问,极好的东东,必须熟练使用。 2、