-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.sh
More file actions
executable file
·42 lines (34 loc) · 1004 Bytes
/
Copy pathimport.sh
File metadata and controls
executable file
·42 lines (34 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
# Check if correct number of arguments is passed
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <input_file> <mongodb_uri> <collection_name>"
exit 1
fi
# Assign arguments to variables
INPUT_FILE=$1
MONGO_URI=$2
COLLECTION_NAME=$3
# Check if the input file exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: File $INPUT_FILE does not exist."
exit 1
fi
# Convert JSON to compact format using jq
OUTPUT_FILE="output.json"
jq --compact-output ".[]" "$INPUT_FILE" > "$OUTPUT_FILE"
# Check if jq command succeeded
if [ $? -ne 0 ]; then
echo "Error: Failed to process $INPUT_FILE with jq."
exit 1
fi
# Import the data into MongoDB using mongoimport
mongoimport --uri "$MONGO_URI" --collection "$COLLECTION_NAME" --file "$OUTPUT_FILE"
# Check if mongoimport command succeeded
if [ $? -eq 0 ]; then
echo "Data imported successfully into collection: $COLLECTION_NAME"
else
echo "Error: Failed to import data into MongoDB."
exit 1
fi
# Cleanup the temporary file
rm -f "$OUTPUT_FILE"