1
This commit is contained in:
@@ -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`
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user