一、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. Windows Server - Tomcat服务器下载、安装、配置环境变量教程

      版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_40881680/articl ...

  2. python接收字符并回显

    # -*- coding: utf-8 -* import serial import time # 打开串口 ser = serial.Serial("/dev/ttyAMA0" ...

  3. 转:Windows系统环境下安装dlib

    原文链接 因为今天安装Face Recognition,需要先按照 dlib .需要在windows环境下做一些图片处理,所以需要在pycharm中配置环境,而其中需要的主要是dlib的安装: 下面说 ...

  4. ElasticSearch(九)e代驾使用Elasticsearch流程设计(Yii1版本)

    一.控制器层的更新.添加.删除 class AddKnowledgeAction extends CAction { //add and update public function actionPo ...

  5. 2019-09-16 curl简单操作

    1.get请求 (使用file_get_contents()函数也可以实现get请求) //http_build_query() 构造一个url字符串 function http_get($url) ...

  6. cs1.6 8倍镜

    地图名字包含awp的是狙击场 打开游戏 搜索方法 开镜搜减小,关镜搜增加 分析数据 开关镜,观察数据变动 修改测试 测试结果 类似的:鼠标XY坐标,也是上减下加,左加右减

  7. logger(一)slf4j简介及其实现原理

    一.slf4j简介 slf4j(Simple logging facade for Java)是对所有日志框架制定的一种规范.标准.接口,并不是一个框架的具体的实现,因为接口并不能独立使用,需要和具体 ...

  8. 0x03 Python logging模块之Formatter格式

    目录 logging模块之Formatter格式 Formater对象 日志输出格式化字符串 LogRecoder对象 时间格式化字符串 logging模块之Formatter格式 在记录日志是,日志 ...

  9. 面向对象(三)--多态、封装、property装饰器

    一.多态与多态性 1.什么是多态 多态指的是同一种类/事物的不同形态 class Animal: def speak(self): pass class People(Animal): def spe ...

  10. Ubuntu 开发环境搭建教程

    Ubuntu 开发环境搭建教程 本文原始地址:https://sitoi.cn/posts/18425.html 更新 sudo apt upgrade sudo apt update 生成本机密钥 ...