Installing MongoDB on Linux and Exporting Data to JSON
1. Preparing the Repository File
To begin, create a repository file for MongoDB on your Red Hat system. This allows you to install MongoDB using yum
.
sudo nano /etc/yum.repos.d/mongodb-org-3.2.repo
Add the following content to the file:
[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. Selecting the Installation Feature
Next, choose the appropriate installation feature based on your needs. Here are the available options:
- mongodb-org: A metapackage that automatically installs the following components:
- 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
, andmongotop
.
- mongodb-org-server: Contains the
To install all components, use the following command:
sudo yum install -y mongodb-org
If you only need the tools, run:
sudo yum install -y mongodb-org-tools
3. Exporting Data to JSON
Finally, use the mongoexport
tool to export your data to JSON format. Here’s how you can do it:
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'
Replace $mongoDBUri
, $db_name
, $collection_name
, and $OUTPUTDIR
with your actual values.
Additional Notes
This guide is part of a series from Tencent Cloud, which focuses on media-sharing plans. If you’re interested in learning more or joining the community, feel free to reach out!
Published on: March 13, 2018