mongodb数据库操作 python+命令行
一、python操作
from bson.objectid import ObjectId import pymongo
client1 = pymongo.MongoClient(host='localhost', port=) from pymongo import MongoClient
client2 = MongoClient('mongodb://localhost:27017/') '''
两种方式都行
''' '''
指定数据库
'''
db = client1.test
db2 = client1['test'] '''
指定集合
'''
collection = db.students
collection2 = db['students'] '''
指定要插入的数据
'''
student = {
'id': '',
'name': 'Jordan',
'age': ,
'gender': 'male'
}
student2 = {
'id': '',
'name': 'Mike',
'age': ,
'gender': 'male'
} '''
保存(可以插入多条) 结果返回id集合
'''
result = collection.insert(student)
print(result) result = collection.insert([student, student2])
print(result)
#=====================================================官方推荐=====================================
result = collection.insert_one(student)
print(result)
print(result.inserted_id) result = collection.insert_many([student, student2])
print(result)
print(result.inserted_ids) '''
================================================================查询===============================
'''
#单条查询
result = collection.find_one({'name': 'Mike'})
print(type(result))
print(result) #根据id查询
result = collection.find_one({'_id': ObjectId('593278c115c2602667ec6bae')})
print(result)
#多条查询
results = collection.find({'age': })
print(results)
for result in results:
print(result)
#查询年龄大于20
results = collection.find({'age': {'$gt': }}) # $lt 小于
#
# $gt 大于
#
# $lte 小于等于
#
# $gte 大于等于
#
# $ne 不等于
#
# $in 在范围内
#
# $nin 不在范围内 #利用正则 #查询以m开头的
results1 = collection.find({'name': {'$regex': '^M.*'}}) # 符号 含义 示例 示例含义
#
# $regex 匹配正则表达式 {'name': {'$regex': '^M.*'}} name以M开头
#
# $exists 属性是否存在 {'name': {'$exists': True}} name属性存在
#
# $type 类型判断 {'age': {'$type': 'int'}} age的类型为int
#
# $mod 数字模操作 {'age': {'$mod': [, ]}} 年龄模5余0
#
# $text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串
#
# $where 高级条件查询 {'$where': 'obj.fans_count == obj.follows_count'} 自身粉丝数等于关注数
'''
===========================================================统计==================================
'''
count = collection.find().count()
print(count)
# 或者统计符合某个条件的数据: count = collection.find({'age': }).count()
print(count) '''
===========================================================排序==================================
升序 pymongo.ASCENDING
降序 pymongo.DESCENDING
'''
results11 = collection.find().sort('name', pymongo.ASCENDING)
print([result['name'] for result in results]) #跳过两个 取两个
results = collection.find().sort('name', pymongo.ASCENDING).skip().limit()
print([result['name'] for result in results]) '''
===========================================================修改==================================
''' condition = {'name': 'Kevin'}
student = collection.find_one(condition)
student['age'] =
result = collection.update(condition, student)
print(result) #推荐
condition = {'name': 'Kevin'}
student = collection.find_one(condition)
student['age'] =
result = collection.update_one(condition, {'$set': student})
print(result)
print(result.matched_count, result.modified_count)
#返回结果----匹配的数据条数和影响的数据条数 condition = {'age': {'$gt': }}
result = collection.update_one(condition, {'$inc': {'age': }})
print(result)
print(result.matched_count, result.modified_count) condition = {'age': {'$gt': }}
result = collection.update_many(condition, {'$inc': {'age': }})
print(result)
print(result.matched_count, result.modified_count) '''
===========================================================删除==================================
''' result = collection.remove({'name': 'Kevin'})
print(result) #推荐
result = collection.delete_one({'name': 'Kevin'})
print(result)
print(result.deleted_count)
result = collection.delete_many({'age': {'$lt': }})
print(result.deleted_count)
二、命令行
# 命令操作
'''
、显示当前数据库服务上的数据库 show dbs; 、切换到指定的数据库进行操作 use mydb 、显示当前数据库的所有集合(collections) show collections; 、查看数据库服务的状态 db.serverStatus(); 、查询指定数据库的统计信息 use admin db.stat() 、查询指定数据库包含的集合名称列表 use test1 db.getCollectionNames() 、统计集合记录数 db.test1.count() 、统计指定条件的记录数 db.test1.find({"name":"yunweicai"}).count() 、查询指定数据库的集合当前可用的存储空间 db.test1.storageSize() 、查询指定数据库的集合分配的存储空间 db.test1.totalSize() 、创建数据库 不需要什么create database的命令,只要使用use命令就可以创建数据库 use test1 、删除数据库 use test1 db.dropDatabase() 、创建集合 可以使用命令db.createCollection(name, { size : ..., capped : ..., max : ... } )创建集合 也可以直接插入一个数据库就直接创建了 db.test1.insert({"name":"mongodb","user":"opcai"}) 、删除集合 db.test1.drop() 、插入记录 db.test1.save({"name":"yunweicai"}) 或者 db.test1.insert({"name":"mongodb","user":"opcai"}) 、查询记录 db.test1.find() find()里面可以指定多个条件进行查询,如果为空,就查询所有的数据 、删除记录 db.test1.remove({"name":"yunweicai"}) 需要指定一个条件,没有条件是不允许删除操作的。
'''
参考:
https://www.cnblogs.com/aademeng/articles/9779271.html
https://baijiahao.baidu.com/s?id=1612042780837847633&wfr=spider&for=pc
mongodb数据库操作 python+命令行的更多相关文章
- 使用Robo 3T 软件管理MongoDB数据库如何执行命令行shell
比如使用命令行的方式查看数据库runoobdb中的sites集合(数据表)中的所有数据 1.在连接名的地方鼠标右键选择“open shell” 2.在出现的shell窗口中输入一下命令行,然后按ctr ...
- mongodb 数据库操作--备份 还原 导出 导入(转)
mongodb 数据库操作--备份 还原 导出 导入 -------------------MongoDB数据导入与导出------------------- 1.导出工具:mongoexport ...
- 快速上手 Python 命令行模块 Click
关于Click? 说下 Click 模块是干啥的,简单说,它就是把我们的 Python 脚本的一些函数,通过 添加带有 Click 关键字的装饰器进行装饰进而将函数调用的形式转化为命令行传参的形式然后 ...
- MySQL数据库操作常用命令
MySQL数据库操作常用命令DOS连接数据库1.安装MySQL配置好环境2.运行cmd命令net start mysql3.找到mysql文件根目录输入命令mysql -h localhost -u ...
- mongoDB 数据库操作
mongoDB 数据库操作 数据库命名规则 . 使用 utf8 字符,默认所有字符为 utf8 . 不能含有空格 . / \ "\0" 字符 (c++ 中会将 "\0&q ...
- 在go中通过cmd调用python命令行参数量级过大问题解决
问题描述如下: 在go中使用cmd调用python命令行 cmd := exec.Command("python", "dimine/Kriging/matrix.py& ...
- Python命令行参数及文件读出写入
看完了柯老板的个人编程作业,虽然是评测组不用做此次作业,但还是想对本次作业涉及到利用Python命令行参数以及进行文件读出写入操作做一个简单的总结.(个人编程作业还是想自己能敲一敲,毕竟我的码力还是小 ...
- python命令行下tab键补全命令
在python命令行下不能使用tab键将命令进行补全,手动输入又很容易出错. 解决:tab.py #/usr/bin/env python # -*- coding:utf-8 -*- ''' 该模块 ...
- C#中隐式操作CMD命令行窗口
原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...
随机推荐
- Mark: 如何用Haskell写一个简单的编译器
作者:aaaron7 链接:https://www.zhihu.com/question/36756224/answer/88530013 如果是用 Haskell 的话,三篇文章足矣. prereq ...
- 费劲周折的Haskell开发环境搭建过程
大概倒腾了一周才搭建好Haskell的开发环境,遇到了很多莫名其妙的问题. 首先,Haskell实在是够冷门,中文网站上的信息实在有限.仅有的一些安装教程分享都感觉不大靠谱,所以我还是直接去外网找吧. ...
- JAVA WEB面试总结
本文目录: 1. 什么是cookie 2. 什么是session 3.什么是Servlet,Servlet生命周期方法 4.JSP隐含对象 5.JSP的四个域对象的作用范围 6.转发和重定向的区别 7 ...
- springmvc4 mybatis 整合 框架源码 bootstrap html5 mysql oracle sqlsever spring SSM
A 调用摄像头拍照,自定义裁剪编辑头像 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统]B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单; 技 ...
- #pragma once和#ifndef
C语言中的头文件只是简单的复制粘贴. C语言中变量.函数.结构体的定义和声明两个过程是分离的.声明通常放在头文件中,为了防止重复声明,需要保证头文件中的内容在构建obj文件时只被包含一次.这可以通过# ...
- MySQL难点语法——子查询
本篇主要通过练习来讲解子查询的知识,进入正题之前,先熟悉数据表,表格的数据可以先不用管,主要是熟悉表格的字段名 这里子查询分为三个部分: 1.where条件子查询 这个子查询和普通的查询没什么区别,主 ...
- Spark实现分组TopN
一.概述 在许多数据中,都存在类别的数据,在一些功能中需要根据类别分别获取前几或后几的数据,用于数据可视化或异常数据预警.在这种情况下,实现分组TopN就显得非常重要了,因此,使用了Spark聚合函数 ...
- go安装配置
https://www.cnblogs.com/wt645631686/p/8124626.html Win10下安装Go开发环境 关于Go语言有多么值得学习,这里就不做介绍了,既然看了这篇文章, ...
- ArmIE的安装
参考:https://developer.arm.com/tools-and-software/server-and-hpc/arm-architecture-tools/arm-instructio ...
- springboot日常问题处理手记
springboot启动问题 1.@Autowired报错Could not autowire. No beans of xxx 解决:只需在DAO接口加上@Component 或者 @Reposit ...