-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.js
More file actions
35 lines (28 loc) · 1.07 KB
/
Copy pathverify.js
File metadata and controls
35 lines (28 loc) · 1.07 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
const { MongoClient } = require('mongodb');
const fs = require('fs');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
async function main() {
try {
await client.connect();
const db = client.db('assignment4_db');
const collection = db.collection('restaurants');
// Read and parse the local restaurants.json file
const rawData = fs.readFileSync('restaurants.json', 'utf8');
const restaurantData = JSON.parse(rawData);
// Clear old data and insert the fresh sample data
await collection.deleteMany({});
await collection.insertMany(restaurantData);
console.log('Successfully loaded restaurant data into MongoDB!');
// --- Practice Queries Area ---
// Example Query: Find all restaurants in Brooklyn
const brooklynRestos = await collection.find({ borough: 'Brooklyn' }).toArray();
console.log('\nPractice Query Result (Restaurants in Brooklyn):');
console.log(brooklynRestos.map(r => r.name));
} catch (error) {
console.error('Error:', error);
} finally {
await client.close();
}
}
main();