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

  1. In the Google Cloud console, go to the project selector page.

    Go to project selector

  2. Select or create a Google Cloud project.

  3. Verify that billing is enabled for your Google Cloud project.

  4. Make sure that you have the following role or roles on the project: Cloud Datastore Owner

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      Buka IAM
    2. Pilih project.
    3. Klik Grant access.
    4. Di kolom New principals, masukkan ID pengguna Anda. Biasanya berupa alamat email untuk Akun Google.

    5. Di daftar Select a role, pilih peran.
    6. Untuk memberikan peran tambahan, klik Tambahkan peran lain, lalu tambahkan setiap peran tambahan.
    7. Klik Simpan.
  5. Instal alat mongosh
  6. 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:

    1. Di konsol Google Cloud , buka halaman Databases.

      Buka Databases

    2. Klik Create a Firestore Database.
    3. Masukkan ID database.
    4. Pilih Edisi Enterprise.
    5. Pilih lokasi untuk database Anda.
    6. Klik Create database.

      Setelah database selesai diinisialisasi, konsol akan membuka Firestore Studio untuk database Anda.

    7. Di bagian Connect to Firestore using an external MongoDB tool, salin string koneksi.

    String koneksi bergantung pada UID database (dibuat 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.

    1. Di konsol Google Cloud , buka halaman Databases.

      Buka Databases

    2. Pilih database dari daftar database.
    3. Di menu navigasi, klik Auth.
    4. Klik Add User.
    5. Masukkan nama pengguna.
    6. Pilih peran Identity and Access Management untuk pengguna.
    7. 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.

    Terhubung menggunakan mongosh

    Gunakan string koneksi, nama pengguna, dan sandi untuk terhubung ke database Anda, lalu 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 dibuat 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