Linux中mongodb安装和导出为json

时间:2022-05-04
本文章向大家介绍Linux中mongodb安装和导出为json,主要内容包括1、编写repo文件、2.选择安装功能、3.导出json、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

采用官方工具导出mongo数据为json格式

文档:https://docs.mongodb.com/manual/reference/program/mongoexport/

可以远程导出,只要有host:port即可。

首先需要安装tools:

在redhat中:

文档:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/

1、编写repo文件

Create a /etc/yum.repos.d/mongodb-org-3.2.repo file so that you can install MongoDB directly, using yum.

[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

2.选择安装功能

一共有以下几种选择

mongodb-org

A metapackage that will automatically install the four component packages listed below.

mongodb-org-server

Contains the mongod daemon and associated configuration and init scripts.

mongodb-org-mongos

Contains the mongos daemon.

mongodb-org-shell

Contains the mongo shell.

mongodb-org-tools

Contains the following MongoDB tools: mongoimport bsondump, mongodump,mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop.

如果全部安装:

sudo yum install -y mongodb-org

如果只安装tools

sudo yum install -y mongodb-org-tools

3.导出json

sudo mongoexport -h $mongoDBUri --db $db_name  -c $collection_name  -q '{
    $or:[
        {"alerts.FamilyFriendly.completed":false},
        {"alerts.GasAndSafety.completed":false},
        {"alerts.Breakfast.completed":false},
        {"alerts.SpokenLanguage.completed":false},
        {"alerts.LocationHighlights.completed":false},
        {"alerts.AccessibilityEnsure.completed":false},
        {"alerts.AirportShuttle.completed":false},
        {"alerts.RoomSize.completed":false},
        {"alerts.Renovations.completed":false}
    ]
}' -f hotelId,alerts   -o $OUTPUTDIR/contentScore-$(date +%F)-e.json --jsonArray
  • -h  host:port
  • --db db
  • -c collections
  • -q query string,查询语法
  • -f  选择导出字段
  • -o  导出文件
  • --jsonArray 导出为数组,默认为{}{}{}...