diff --git a/src/app/api/import-json/route.ts b/src/app/api/import-json/route.ts index fb7ad3d..bab130b 100644 --- a/src/app/api/import-json/route.ts +++ b/src/app/api/import-json/route.ts @@ -148,19 +148,26 @@ export async function POST(request: NextRequest) { // Convert MongoDB extended JSON to proper MongoDB documents const documents = rawDocuments.map((doc: any) => convertMongoExtendedJSON(doc)); + // Get collection name from filename (without .json extension) + const collectionName = file.name.replace(/\.json$/i, ''); + // Get MongoDB collection const db = await getDatabase(); - const collection = db.collection('observations'); + const collection = db.collection(collectionName); + + // Drop existing collection if it exists + await collection.drop().catch(() => {}); // Insert documents const result = await collection.insertMany(documents); return NextResponse.json({ success: true, - message: `Successfully imported ${result.insertedCount} observations from ${file.name}`, + message: `Successfully imported ${result.insertedCount} documents into collection '${collectionName}' from ${file.name}`, inserted_count: result.insertedCount, inserted_ids: Object.values(result.insertedIds), filename: file.name, + collection_name: collectionName, size: `${(file.size / 1024).toFixed(2)} KB` });