-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsert.js
More file actions
38 lines (32 loc) · 1.26 KB
/
insert.js
File metadata and controls
38 lines (32 loc) · 1.26 KB
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
const { MongoClient, ObjectId } = require('mongodb');
const uri = "mongodb://127.0.0.1/vehahavtem";
async function updateDonations() {
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
try {
await client.connect();
const database = client.db('vehahavtem');
const donationsCollection = database.collection('donations');
const donorsCollection = database.collection('donors');
// Find all donations
const donations = await donationsCollection.find({}).toArray();
for (let donation of donations) {
if (donation.donor && typeof donation.donor === 'object') {
const donorEmail = donation.donor.email;
const donor = await donorsCollection.findOne({ email: donorEmail });
if (donor) {
// Update donation to reference donor's ObjectId
await donationsCollection.updateOne(
{ _id: donation._id },
{ $set: { donor: donor._id } }
);
console.log(`Updated donation ${donation._id} to reference donor ${donor._id}`);
} else {
console.log(`No matching donor found for donation ${donation._id}`);
}
}
}
} finally {
await client.close();
}
}
updateDonations().catch(console.error);