一、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+命令行的更多相关文章

  1. 使用Robo 3T 软件管理MongoDB数据库如何执行命令行shell

    比如使用命令行的方式查看数据库runoobdb中的sites集合(数据表)中的所有数据 1.在连接名的地方鼠标右键选择“open shell” 2.在出现的shell窗口中输入一下命令行,然后按ctr ...

  2. mongodb 数据库操作--备份 还原 导出 导入(转)

    mongodb 数据库操作--备份 还原 导出 导入   -------------------MongoDB数据导入与导出------------------- 1.导出工具:mongoexport ...

  3. 快速上手 Python 命令行模块 Click

    关于Click? 说下 Click 模块是干啥的,简单说,它就是把我们的 Python 脚本的一些函数,通过 添加带有 Click 关键字的装饰器进行装饰进而将函数调用的形式转化为命令行传参的形式然后 ...

  4. MySQL数据库操作常用命令

    MySQL数据库操作常用命令DOS连接数据库1.安装MySQL配置好环境2.运行cmd命令net start mysql3.找到mysql文件根目录输入命令mysql -h localhost -u ...

  5. mongoDB 数据库操作

    mongoDB 数据库操作 数据库命名规则 . 使用 utf8 字符,默认所有字符为 utf8 . 不能含有空格 . / \ "\0" 字符 (c++ 中会将 "\0&q ...

  6. 在go中通过cmd调用python命令行参数量级过大问题解决

    问题描述如下: 在go中使用cmd调用python命令行 cmd := exec.Command("python", "dimine/Kriging/matrix.py& ...

  7. Python命令行参数及文件读出写入

    看完了柯老板的个人编程作业,虽然是评测组不用做此次作业,但还是想对本次作业涉及到利用Python命令行参数以及进行文件读出写入操作做一个简单的总结.(个人编程作业还是想自己能敲一敲,毕竟我的码力还是小 ...

  8. python命令行下tab键补全命令

    在python命令行下不能使用tab键将命令进行补全,手动输入又很容易出错. 解决:tab.py #/usr/bin/env python # -*- coding:utf-8 -*- ''' 该模块 ...

  9. C#中隐式操作CMD命令行窗口

    原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...

随机推荐

  1. 基于vue+springboot+docker网站搭建【七】制作后端spring-boot的docker镜像部署

    制作spring-boot的docker镜像并部署 一.下载后端项目:https://github.com/macrozheng/mall 二.修改mall-admin项目的配置文件 修改applic ...

  2. spring AOP的两种配置

    xml配置 定义要被代理的方法的接口 public interface TestAop { public void print(String s); } 实现上述接口 public class Tes ...

  3. EHLIB 安装方法

    Ehlib安装方法  路人甲 2010-05-05 23:01:37 安装文件自带的Readme.txt中的安装过程如下: 1. Delphi 5.x - 7.x, Delphi 9.X Win32, ...

  4. 【转】解决Oracle 11g在用EXP导出时,空表不能导出

    一.问题原因: 11G中有个新特性,当表无数据时,不分配segment,以节省空间 .insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segme ...

  5. 集成学习 - Bagging

    认识 Bagging 的全称为 (BootStrap Aggregation), 嗯, 咋翻译比较直观一点呢, 就有放回抽样 模型训练? 算了, 就这样吧, 它的Paper是这样的: Algorith ...

  6. ETL DAG调度策略

    1.目前etl的fetch task策略是基于任务子孙任务数和任务优先级获得task list 2.然后遍历task list 查看任务是否具备执行条件 集群资源校验(yarn/hdfs)<如果 ...

  7. 使用PSCI机制的SMP启动分析

    其他core的入口 文件:arch/arm64/kernel/head.S secondary_entry: 在从bl31切到EL1上的Linux Kernel后: 第595行,在el2_setup中 ...

  8. linux查看磁盘类型(是否SSD盘)

    介绍两种方法: 第一种: cat /sys/block/sda/queue/rotational 注意: 命令中的sba是你的磁盘名称,可以通过df命令查看磁盘,然后修改成你要的 结果: 返回0:SS ...

  9. 【JSTL】JSTL标签库的常用标签

    一.JSTL技术 1.JSTL概述 JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样 ...

  10. leetcode4 Median of Two Sorted Arrays学习记录

    学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge- ...