From 6fd4341adae94cb0feec206af7c25c0990a1bdd0 Mon Sep 17 00:00:00 2001 From: medina474 Date: Mon, 6 Oct 2025 16:52:56 +0200 Subject: [PATCH] mongo init --- compose.yaml | 1 + json/export.sql | 20 ++++++++++++++++++++ mongo-init/01-import.sh | 8 ++++++++ 3 files changed, 29 insertions(+) create mode 100644 json/export.sql create mode 100644 mongo-init/01-import.sh diff --git a/compose.yaml b/compose.yaml index 246f641..c4b94d3 100644 --- a/compose.yaml +++ b/compose.yaml @@ -92,6 +92,7 @@ services: volumes: - mongodb_configdb:/data/configdb - mongodb_data:/data/db + - ./mongo-init:/docker-entrypoint-initdb.d ports: - "27017:27017" environment: diff --git a/json/export.sql b/json/export.sql new file mode 100644 index 0000000..25dc7eb --- /dev/null +++ b/json/export.sql @@ -0,0 +1,20 @@ +DO $$ +DECLARE + r RECORD; + file_path TEXT; +BEGIN + FOR r IN SELECT id, caracteristiques FROM item + LOOP + file_path := format('/tmp/json/%s.json', r.id); + + -- Écrit le fichier (nécessite superutilisateur) + PERFORM pg_catalog.pg_write_file( + file_path, + r.caracteristiques::text, + false -- false = écrasement (pas d'append) + ); + + RAISE NOTICE 'Exporté : %', file_path; + END LOOP; +END; +$$ LANGUAGE plpgsql; diff --git a/mongo-init/01-import.sh b/mongo-init/01-import.sh new file mode 100644 index 0000000..a275a14 --- /dev/null +++ b/mongo-init/01-import.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +for file in /docker-entrypoint-initdb.d/*.json; do + collection=$(basename "$file" .json) + echo "Importing $collection..." + mongoimport --db mydb --collection "$collection" --file "$file" --jsonArray +done