DATABASE_URL="mongodb://localhost:27017/tourTrek?replicaSet=rs0"
Hoặc
DATABASE_URL="mongodb://localhost:27017/tourTrek"
Ví nếu không chạy nó sẽ có lỗi sau
prisma\schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
age Int
}
.env
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
#DATABASE_URL="mongodb://admin:12345678@localhost:27017/mydatabase?authSource=admin"
DATABASE_URL="mongodb://localhost:27017/mydatabase?replicaSet=rs0"
Dưới đây là hướng dẫn từng bước để cài đặt MongoDB, tạo database, kết nối với Node.js và sử dụng Prisma ORM trên Windows.
🔹 1. Cài Đặt MongoDB trên Windows
📌 Bước 1: Tải và Cài Đặt MongoDB
1️⃣ Truy cập Trang tải MongoDB và chọn bản MongoDB Community.
2️⃣ Chạy file .msi và làm theo hướng dẫn.
3️⃣ Chọn "Complete" để cài đặt đầy đủ MongoDB.
4️⃣ Chọn "Install MongoDB as a Service" để MongoDB tự động chạy nền.
5️⃣ Hoàn tất và nhấn "Finish".
📌 Bước 2: Kiểm Tra Cài Đặt MongoDB
Mở Command Prompt (CMD) và nhập:
mongod --version
Nếu hiển thị phiên bản MongoDB, có nghĩa là đã cài thành công.
📌 Bước 3: Khởi Động MongoDB
Mặc định, MongoDB sẽ tự động chạy nền. Nếu chưa chạy, bạn có thể khởi động bằng:
2️⃣ Mở file prisma/schema.prisma và cập nhật như sau:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model User {
id String @id @default(auto()) @map("_id")
name String
age Int
}
✅ Cài đặt MongoDB trên Windows
✅ Khởi động MongoDB & tạo database
✅ Tạo project Node.js & cài đặt Prisma
✅ Cấu hình Prisma với MongoDB
✅ Viết code Node.js để thêm & lấy dữ liệu
🔥 Bạn đã hoàn thành! 🎉 Bây giờ bạn có thể dùng MongoDB & Prisma trong các dự án của mình. Nếu gặp lỗi, cứ hỏi mình nhé! 🚀