This commit is contained in:
Kar
2026-02-24 23:34:00 +05:30
parent e6ea620e1b
commit a1a787ec81

View File

@@ -148,19 +148,26 @@ export async function POST(request: NextRequest) {
// Convert MongoDB extended JSON to proper MongoDB documents // Convert MongoDB extended JSON to proper MongoDB documents
const documents = rawDocuments.map((doc: any) => convertMongoExtendedJSON(doc)); 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 // Get MongoDB collection
const db = await getDatabase(); 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 // Insert documents
const result = await collection.insertMany(documents); const result = await collection.insertMany(documents);
return NextResponse.json({ return NextResponse.json({
success: true, 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_count: result.insertedCount,
inserted_ids: Object.values(result.insertedIds), inserted_ids: Object.values(result.insertedIds),
filename: file.name, filename: file.name,
collection_name: collectionName,
size: `${(file.size / 1024).toFixed(2)} KB` size: `${(file.size / 1024).toFixed(2)} KB`
}); });