一、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. ASP.NET MVC EF 连接数据库(二)-----Model First

    Model first (VS2015 ,Sql Server2014) 新建MVC项目     右键product ,新增标量属性(数据库表中的字段)   Ctrl + S 保存页面,右键“根据模型 ...

  2. Spring Boot2(八):性感banner,在线发牌

    本文在个人技术博客[鸟不拉屎]同步发布,详情可猛戳 亦可扫描文章末尾二维码关注个人公众号[鸟不拉屎] emmm,没有啥前言 玩过SpringBoot的都知道,SpringBoot启动的时候,默认会在控 ...

  3. C++运算符重载学习总结

    在C ++中,我们可以使运算符适用于用户定义的类. 这意味着C ++能够为运算符提供数据类型的特殊含义,这种能力称为运算符重载. 例如,我们可以在像String这样的类中重载运算符'+',这样我们就可 ...

  4. Django框架(七)-- 模板层:模板导入、模板继承、静态文件

    一.模板导入 要复用一个组件,可以将该组件写在一个文件中,在使用的时候导入即可 在模板中使用 1.语法 {% include '模板名字' %} 2.使用 ad.html页面 <div clas ...

  5. hadoop hdfs 有内网、公网ip后,本地调试访问不了集群解决

    问题背景: 使用云上的虚拟环境搭建测试集群,导入一些数据,在本地idea做些debug调试,但是发现本地idea连接不上测试环境 集群内部配置hosts映射是内网映射(内网ip与主机名映射),本地只能 ...

  6. Mock.js数据模拟

    数据来源方式: 为什么要用mockjs 实际开发中,前后端分离,前端需要后端的接口去完成页面的渲染,但是并不能等到后端成员写完接口再开始进行测试.大部分情况下,前后端需要同时进行开发.因此便需要moc ...

  7. 安装glibc

    wget http://ftp.gnu.org/gnu/glibc/glibc-2.23.tar.gztar -zxvf glibc-2.23.tar.gz cd glibc-2.23 mkdir b ...

  8. Linux中的关机操作

    shutdown -h now //马上停止服务进行关机 shutdown -h  12:00 .//在12点后进行关机 shutdown -h +10   //在10分钟后进行关机 shutdown ...

  9. Scrapy笔记05- Item详解

    Scrapy笔记05- Item详解 Item是保存结构数据的地方,Scrapy可以将解析结果以字典形式返回,但是Python中字典缺少结构,在大型爬虫系统中很不方便. Item提供了类字典的API, ...

  10. 出现 sudo: unable to resolve host XXX 信息解决办法

    Ubuntu环境,  每次执行sudo 就出现这个警告讯息:sudo: unable to resolve host XXX虽然sudo 还是可以正常执行,是机器在反解上的问题, 所以就直接从/etc ...