|
| 1 | +js |
1 | 2 |
|
2 | | ---- |
| 3 | +db = db.getSiblingDB("fullstack_app"); |
3 | 4 |
|
4 | | -## **6️⃣ `mongo-init.js` (sample)** |
| 5 | +// Seed users |
| 6 | +if (db.users.countDocuments() === 0) { |
| 7 | + db.users.insertMany([ |
| 8 | + { |
| 9 | + name: "Admin User", |
| 10 | + email: "admin@example.com", |
| 11 | + password: "admin123", // hash in production |
| 12 | + role: "admin" |
| 13 | + }, |
| 14 | + { |
| 15 | + name: "Test User", |
| 16 | + email: "user@example.com", |
| 17 | + password: "user123", |
| 18 | + role: "user" |
| 19 | + } |
| 20 | + ]); |
| 21 | + print("✅ Seeded users collection"); |
| 22 | +} |
5 | 23 |
|
6 | | -```js |
7 | | -// mongo-init.js - Seed data for MongoDB |
8 | | -db = db.getSiblingDB('fullstack_app'); |
| 24 | +// Seed items |
| 25 | +if (db.items.countDocuments() === 0) { |
| 26 | + db.items.insertMany([ |
| 27 | + { title: "First Item", description: "This is a seeded item." }, |
| 28 | + { title: "Second Item", description: "Another example entry." } |
| 29 | + ]); |
| 30 | + print("✅ Seeded items collection"); |
| 31 | +} |
9 | 32 |
|
10 | | -db.users.insertMany([ |
11 | | - { username: "admin", password: "admin123", role: "admin" }, |
12 | | - { username: "user1", password: "user123", role: "user" } |
13 | | -]); |
14 | | - |
15 | | -db.products.insertMany([ |
16 | | - { name: "Widget", price: 9.99, stock: 100 }, |
17 | | - { name: "Gadget", price: 19.99, stock: 50 } |
18 | | -]); |
19 | 33 |
|
20 | 34 | print("✅ MongoDB seed complete"); |
0 commit comments