-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncHashtagsToMongo.js
More file actions
44 lines (35 loc) · 1.46 KB
/
syncHashtagsToMongo.js
File metadata and controls
44 lines (35 loc) · 1.46 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
39
40
41
42
43
44
// syncHashtagsToMongo.js
import { db } from './src/lib/firebase/index.js'; // Firestore client
import { getMongoClient } from './src/lib/mongo/index.js'; // MongoDB helper
import { collection, getDocs } from 'firebase/firestore';
async function syncHashtags() {
try {
console.log('Connecting to MongoDB...');
const mongoClient = await getMongoClient();
const mongoDb = mongoClient.db('your_mongo_db_name'); // replace with your DB name
const hashtagsCollection = mongoDb.collection('hashtags');
console.log('Fetching tweets from Firestore...');
const tweetsSnapshot = await getDocs(collection(db, 'tweets'));
let syncedCount = 0;
for (const tweetDoc of tweetsSnapshot.docs) {
const tweetData = tweetDoc.data();
const hashtags = tweetData.hashtags;
if (!hashtags || !hashtags.length) continue;
for (const tag of hashtags) {
// Upsert into Mongo: add tweet ID to the hashtag document only if it doesn't exist
await hashtagsCollection.updateOne(
{ tag, tweets: { $ne: tweetDoc.id } }, // only if tweet ID is not already in the array
{ $addToSet: { tweets: tweetDoc.id } },
{ upsert: true }
);
}
syncedCount++;
}
console.log(`✅ Synced ${syncedCount} tweets with hashtags to MongoDB`);
await mongoClient.close();
console.log('MongoDB connection closed');
} catch (err) {
console.error('Error syncing hashtags:', err);
}
}
syncHashtags();