Purpose: compile a shell script to export data from mongoDb to a csv file. Requirements: 1. To avoid exporting massive amounts of data, you only need to filter out the data for the current month and the data for the previous month. 2. Because the data of the day is incomplete, We need to exclude the data of the day. The following is an example --------------------------------------------------------------------------#! /Bin/bash # create and 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 $ baseDatedeclare-I baseTimebaseTime = $ ('date -- date "$ baseDate" + "% s" '* 1000 ))
# Export the data 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.csvElse$ ExportCmd-d $1-c $2-f $3-o $ mypath/export 2.csvFi}
# Export all data of the first table echo export data from $ baseDateecho 'export ossDev. t_User 'fields = "_ id, email, registerDate, photoId" exportData "ossDev" "T_User" $ fields
# Export the sendTime of the second table to echo 'export snsDev. t_Comment 'fields = "_ id, del, from, mbId, mbOwner, nwId, ownerId, sendTime" exportData "snsDev" "T_Comment" $ fields "sendTime" begin is saved. 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.