Panduan memulai: Membuat database dan terhubung ke database tersebut
Pelajari cara membuat database Firestore dengan kompatibilitas MongoDB dan terhubung ke database tersebut dengan alat mongosh
.
Sebelum memulai
-
In the Google Cloud console, go to the project selector page.
-
Select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Cloud Datastore Owner
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Buka IAM - Pilih project.
- Klik Grant access.
-
Di kolom New principals, masukkan ID pengguna Anda. Biasanya berupa alamat email untuk Akun Google.
- Di daftar Select a role, pilih peran.
- Untuk memberikan peran tambahan, klik Tambahkan peran lain, lalu tambahkan setiap peran tambahan.
- Klik Simpan.
- Menginstal alat
mongosh
Buat database Firestore dengan kompatibilitas MongoDB dan ambil string koneksi
Di konsol Google Cloud , buat database edisi Firestore Enterprise baru. Firestore dengan kompatibilitas MongoDB memerlukan edisi Enterprise Firestore:-
Di konsol Google Cloud , buka halaman Databases.
- Klik Create a Firestore database.
- Masukkan ID Database.
- Pilih Edisi Enterprise.
- Pilih lokasi untuk database Anda.
Klik Create database.
Setelah database selesai diinisialisasi, konsol akan membuka Firestore Studio untuk database Anda.
-
Di bagian Connect to Firestore using an external MongoDB tool, salin string koneksi.
String koneksi bergantung pada UID database (yang dihasilkan sistem) dan lokasi database:
UID.LOCATION.firestore.goog
Membuat pengguna untuk autentikasi SCRAM
Di konsol Google Cloud , buat pengguna database baru dan tetapkan izin Pengelolaan Identitas dan Akses pengguna.
-
Di konsol Google Cloud , buka halaman Databases.
- Pilih database dari daftar database.
- Di menu navigasi, klik Auth.
- Klik Add user.
- Masukkan nama pengguna.
- Pilih peran Identity and Access Management untuk pengguna.
- Klik Create. Database membuat pengguna dan menampilkan sandi yang dibuat untuk pengguna tersebut. Salin dan simpan sandi ini. Anda tidak akan dapat mengambil sandi ini nanti..
Sambungkan menggunakan
mongosh
Gunakan string koneksi, nama pengguna, dan sandi untuk terhubung ke database Anda, jalankan
mongosh
secara lokal dengan opsi konfigurasi berikut.mongosh 'mongodb://USERNAME:PASSWORD@CONNECTION_STRING:443/DATABASE_ID?loadBalanced=true&authMechanism=SCRAM-SHA-256&tls=true&retryWrites=false'
Ganti kode berikut:
- USERNAME: nama pengguna database yang Anda buat.
- PASSWORD: sandi yang dihasilkan untuk pengguna database yang Anda buat.
- CONNECTION_STRING: string koneksi database.
- DATABASE_ID: ID database.
Setelah terhubung, Anda dapat membuat dan membaca data, misalnya:
db.pages.insertOne({ message: "Hello World!"}) db.pages.find({}) exit
Langkah berikutnya
- Lihat daftar fitur yang didukung
- Mempelajari perbedaan perilaku di Firestore dengan kompatibilitas MongoDB
- Mempelajari metode autentikasi tambahan
-