Purpose: compile a shell script to export data from mongoDb to a csv file. Requirements: 1. To avoid exporting large amounts of data, you only need to filter out the current month
Purpose: compile a shell script to export data from mongoDb to a csv file. Requirements: 1. To avoid exporting large amounts of data, you only need to filter out the current month
Purpose: compile a shell script to export data from mongoDb to a csv file.
Requirements:
1. To avoid exporting huge amounts of data, you only need to filter out the data of the current month and the data of the previous month.
2. Because the data of the current day is incomplete, You need to exclude the data of the current day.
The following is an example
--------------------------------------------------------------------------
#! /Bin/bash
# Create a save directory
Mypath = micAnalyDir/'date + % Y/% m % d'
Mkdir-p $ mypath
# Calculate the filter time
BaseDate = 'date -- date' 1 months ago '+' % m/01/% y''
Echo $ baseDate
Declare-I baseTime
BaseTime = $ ('date -- date "$ baseDate" + "% s" '* 1000 ))
# Exporting data from a specified data table
Function exportData (){
ExportCmd = "/kingdee/mongodb/bin/export -- csv"
If [$ #-gt 3]; then
# Note that baseTime and endTime cannot be enclosed in quotation marks; otherwise, an error is reported.
$ ExportCmd-d $1-c $2-f $3-q \
"{\" $4 \ ": {\" \ $ gte \ ": {\" \ $ date \ ": $ baseTime },\
\ "\ $ Lt \": {\ "\ $ date \": $ endTime }}"-o $ mypath/export 2.csv
Else
$ ExportCmd-d $1-c $2-f $3-o $ mypath/export 2.csv
Fi
}
# Export all data in the first table
Echo export data from $ baseDate
Echo 'export ossDev. t_user'
Fields = "_ id, email, registerDate, photoId"
ExportData "ossDev" "T_User" $ fields
# Export the sendTime of the second table as the data for the current month and the previous month
Echo 'export snsDev. t_comment'
Fields = "_ id, del, from, mbId, mbOwner, nwId, ownerId, sendTime"
ExportData "snsDev" "T_Comment" $ fields "sendTime"
----------------------------------------------------------------------------
Save it as a. sh file, use chmod + x to set the script to executable, and then use the crontab command to add the script to the scheduled task.