代码编织梦想

背景简介

开发背景

  • 开发环境:
    • docker中容器名:mongo(版本:mongo:4.2.3)
  • mongo中相关配置:
    • 数据库(database):audit
    • 表(或集合collection):t_audit

条件操作符介绍

单个

操作符说明示例类似于sql
$gt大于{"_id" : {$gt : 3 }}where _id > 3
$lt小于{"_id" : {$lt : 3 }}where _id < 3
$lte不大于{"_id" : {$lte : 3 }}where _id <= 3
$gte不小于{"_id" : {$lte : 3 }}where _id >= 3

组合

操作符说明示例类似于sql
$gt 和 $lt数字区间段{"_id" : {$gt : 3, $lt: 10}}where _id > 3 and _id < 10

时间操作

Date()

类型为:字符串

//Wed Dec 19 2012 01:03:25 GMT-0500 (EST)
Date() method which returns the current date as a string.

new Date()

类型为:字符串ISODate

//ISODate("2012-12-19T06:01:17.171Z")
new Date() constructor which returns a Date object using the ISODate() wrapper.

ISODate()

类型为:字符串ISODate

ISODate() constructor which returns a Date object using the ISODate() wrapper.

mongoexport 简介

官方解释:Export data from MongoDB in CSV or JSON format.
以json或csv格式导出数据

Usage:
  mongoexport <options>

Export data from MongoDB in CSV or JSON format.

mongoexport命令 参数比较多(不懂的时候,在shell中键入 mongoexport --help即可)

verbosity options:
  -v, --verbose=<level>                           more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)
      --quiet                                     hide all log output

connection options:
  -h, --host=<hostname>                           mongodb host to connect to (setname/host1,host2 for replica sets)
      --port=<port>                               server port (can also use --host hostname:port)

ssl options:
      --ssl                                       connect to a mongod or mongos that has ssl enabled
      --sslCAFile=<filename>                      the .pem file containing the root certificate chain from the certificate authority
      --sslPEMKeyFile=<filename>                  the .pem file containing the certificate and key
      --sslPEMKeyPassword=<password>              the password to decrypt the sslPEMKeyFile, if necessary
      --sslCRLFile=<filename>                     the .pem file containing the certificate revocation list
      --sslAllowInvalidCertificates               bypass the validation for server certificates
      --sslAllowInvalidHostnames                  bypass the validation for server name
      --sslFIPSMode                               use FIPS mode of the installed openssl library

authentication options:
  -u, --username=<username>                       username for authentication
  -p, --password=<password>                       password for authentication
      --authenticationDatabase=<database-name>    database that holds the user's credentials
      --authenticationMechanism=<mechanism>       authentication mechanism to use

kerberos options:
      --gssapiServiceName=<service-name>          service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
      --gssapiHostName=<host-name>                hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>)

namespace options:
  -d, --db=<database-name>                        database to use
  -c, --collection=<collection-name>              collection to use

uri options:
      --uri=mongodb-uri                           mongodb uri connection string

output options:
  -f, --fields=<field>[,<field>]*                 comma separated list of field names (required for exporting CSV) e.g. -f "name,age"
      --fieldFile=<filename>                      file with field names - 1 per line
      --type=<type>                               the output format, either json or csv (defaults to 'json') (default: json)
  -o, --out=<filename>                            output file; if not specified, stdout is used
      --jsonArray                                 output to a JSON array rather than one object per line
      --pretty                                    output JSON formatted to be human-readable
      --noHeaderLine                              export CSV data without a list of field names at the first line
      --jsonFormat=<type>                         the extended JSON format to output, either canonical or relaxed (defaults to 'relaxed') (default: relaxed)

querying options:
  -q, --query=<json>                              query filter, as a JSON string, e.g., '{x:{$gt:1}}'
      --queryFile=<filename>                      path to a file containing a query filter (JSON)
  -k, --slaveOk                                   allow secondary reads if available (default true) (default: false)
      --readPreference=<string>|<json>            specify either a preference mode (e.g. 'nearest') or a preference json object (e.g. '{mode: "nearest", tagSets: [{a: "b"}], maxStalenessSeconds:
                                                  123}')
      --forceTableScan                            force a table scan (do not use $snapshot or hint _id). Deprecated since this is default behavior on WiredTiger
      --skip=<count>                              number of documents to skip
      --limit=<count>                             limit the number of documents to export
      --sort=<json>                               sort order, as a JSON string, e.g. '{x:1}'
      --assertExists                              if specified, export fails if the collection does not exist (default: false)

按时间段导出

需求:将数据库audit,集合t_audit中2020-12-10T16:00:00.000Z ~ 2020-12-10T16:59:59.999Z

mongoexport 在容器内生成csv

mongoexport -h localhost -u root -p 123456 --authenticationDatabase=admin --authenticationMechanism=SCRAM-SHA-1 --port 27017 -d audit -c t_audit --type=csv  -q '{"createTime": {"$gte": {"$date" : "2020-12-09T16:00:00.000Z"}}}' -f _id,userId,userName,accessIp,accessUrl,description,createTime,pushTime  -o /root/audit20201210_1.csv

虽然上一节中有mongoexport的参数说明,但还是再重复一下~

  • -h 主机
  • -u 用户
  • -p 密码
  • --authenticationDatabase 保存用户凭据的数据库
  • --authenticationMechanism 使用的身份验证机制
  • –port mongo服务端口号
  • -d 数据库名
  • -c 集合(表)名
  • –type 导出格式
  • -q 查询条件
  • -f 查询字段(用逗号隔开)
  • -o 导出文件名(容器内绝对路径)

docker cp将容器内csv复制到主机上

将容器中csv文件复制到主机/root/db/data目录里

docker cp mongo:/mongo/export.csv /root/db/data/

遇到的问题

query无法识别 $gt

出现问题:Failed: error parsing query as Extended JSON: invalid JSON input. Position: 20. Character: $

问题点:-q后面接收的参数非json格式,需要检查参数

–query=<json> query filter, as a JSON string, e.g., ‘{x:{$gt:1}}’
–queryFile=<filename>

解决方案:$gte 加上双引号

-q ‘{"_id": {$gte: 100}}’ -->> 改成 -q ‘{"_id": {"$gte": 100}}’

query无法识别 ISODate

出现问题:Failed: error parsing query as Extended JSON: invalid JSON input. Position: 20. Character: I

问题点:query参数无法识别ISODate

解决方案:$date
(在query条件中尝试过ISODate的各路写法,但是一直都会抛非法json格式,最后在万能的stackoverflow上找到了 $date,然后就愉快的解决了问题!)

 -q '{"createTime": {"$gte": {"$date" : "2020-12-09T16:00:00.000Z"}}}'

初次接触mongo,对它知之甚少,相关需求一来就懵圈,看来还是要不断摸索学习啊~~

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

Mysql学习-爱代码爱编程

day01——今日内容 数据库的基本概念 MySQL数据库软件 安装卸载配置 SQL public static void main(){} 数据库的基本概念 1. 数据库的英文单词: DataBase 简称 : DB 2. 什么数据库? * 用于存储和管理数据的仓库。 3. 数据库的特点: 1. 持久化存储数据的。其实数据库就是一

Redis入门操作-爱代码爱编程

目录 学习目标概念应用场景安装启动和配置启动服务端启动客户端服务端配置入门操作redis支持的数据类型string基本操作扩展操作扩展操作注意事项key的命名规范hash基本操作扩展操作注意事项应用场景list基本操作扩展操作注意事项应用场景set基本操作注意事项应用场景实践案例key的通用操作db常用指令 学习目标 1.能够独立

Python笔记之hashlib加密!!!-爱代码爱编程

文章目录 1.加密算法的介绍1.1Hash1.2MD5算法1.2.1什么是MD5算法?1.2.2MD5功能1.2.3MD5算法的特点1.2.4MD5算法是否可逆?1.2.5MD5用途1.3SHA-12.MD5与SHA-1的比较3.Python中关于算法的一些例子。 1.加密算法的介绍 关于加密算法的小故事: 在我们上网的时候会注册账号,密码等

Alibaba最新《Java架构核心宝典》限时开放下载,互联网主流技术详解总结,提升技术能力的必备宝典!-爱代码爱编程

导言 什么是架构师?对于程序员来说,聊架构是一个永不过时的话题。实际上,每一家公司都有自己对架构师不同的定位,因为不同的公司,所处的阶段、业务模式以及应用场景都不一样,因此对架构师的要求不一样,所以定位也就不同。 但是,无论如何,架构师除了优秀的合作能力以及清晰的思路头脑以外,过硬的技术基础也是很有必要的,大型的互联网公司对架构师的技术要求也是非常高的

mysql使用group by的小坑你知道吗?(mysql版本8.0.19)-爱代码爱编程

前言: 有很多东西经常在用,但是却有很多细节没有注意到,有一天突然发现原有还有这种限制呢,发现原来小丑是自己。 今天这篇文章就讲解下mysql使用group by和distinct关键字容易出现的问题! 正文: 废话不多说,还是先复现问题! 一、复现问题 1.我们先新建一张表,如下图所示  2.我们按名字分组统计下数量,查出改名字的i

Java程序员2021年金三银四面试必备:框架+性能优化+微服务+分布式(学习路线+学习笔记+面试真题)-爱代码爱编程

导言 今年似乎因为疫情影响,时间过得特别快,对于需要跳槽换工作的人来,更觉得有些突然,似乎金三银四和金九银四还没开始准备好,就匆匆过去。加上今年的大环境不佳,所以大部分的人在今年的招聘旺季都没有收获到好的结果。 今天分享的主题则是由 一位阿里P7的面试心得,通过32天的高效突击训练,成功拿下offer的学习方法。 篇章分为三大章节,可以根据自己所需来

SpringMvc整合MongoDB 初识(一)-爱代码爱编程

MongoDb初识(1) 基本概念解析 MongoDb属于非关系型数据库之一,mongodb中 基本概念是 文档、集合、数据库,下表是从菜鸟教程里面复制的关于MongoDb的一些常见理念,可能写的不好,以后再对mongo认知更多的基础上,再次修改和提升文档编辑。 SQL术语/概念MongoDB术语/概念解释/说明databasedatabase数据

centos下安装mangodb(社区版)-爱代码爱编程

1下载 mongodb.com 这里使用mongodb-linux-x86_64-4.1.3.tar 2.安装 解压文件: tar -xvf mongodb-linux-x86_64-4.1.3.tar 解压后得到对应的文件夹,进入文件夹后bin目录下是mongo启动脚本 启动mongo:进入bin目录,输入命令./mon

使用transporter实现在线迁移MongoDB-爱代码爱编程

使用transporter实现在线迁移MongoDB transporter简介安装Transporter在线迁移工具准备好必要参数transporter的使用 transporter简介 在某些场景下,为了保证业务的连续性,MongoDB的迁移过程,希望源端数据库与目的端数据都保证在线状态。(不能宕机)此时可以使用开源工具transport

MongoDB 安装以及简单使用-爱代码爱编程

  MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。   MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。 文章目录 一、mongodb 安装二、java 调用 mongodb 简单使用总结 一、

MongoDB下载及配置-爱代码爱编程

文章目录 MongoDB官方下载安装设置配置文件指定端口号一、下载MongoDB包1.百度一下2.首页右上角3.进入下载中心,选择Server4.进入Server下载选项二、解压1.安装包2.解压展开如下图所示三、启动四、配置文件 MongoDB官方下载安装设置配置文件指定端口号 下面是相信说明,如有疑问请提问。 一、下载MongoDB包

Docker部署Mongo集群 一主两从-爱代码爱编程

环境: 1、准备一个linux服务器(我这边是centos系统) 2、确保有docker和docker-compose环境 搭建流程 第一步(主要是配置mongo容器挂载的位置,生成一个认证文件,三个mongo共用) // 1 登录进自己的服务器 切到根目录 cd / // 2 创建docker 文件夹 mkdir docker // 3 进入