Cloud Firestore 시작하기

빠른 시작에서는 Cloud Firestore를 설정하고 데이터를 추가한 후 Firebase Console에서 방금 추가한 데이터를 확인하는 방법을 볼 수 있습니다.

Cloud Firestore 데이터베이스 만들기

  1. Firebase 프로젝트를 아직 만들지 않았으면 Firebase Console에서 프로젝트 추가를 클릭한 후 화면에 표시된 안내를 따라 Firebase 프로젝트를 만들거나 기존 GCP 프로젝트에 Firebase 서비스를 추가합니다.

  2. Firebase ConsoleCloud Firestore 섹션으로 이동합니다. 기존 Firebase 프로젝트를 선택하라는 메시지가 표시됩니다. 데이터베이스 만들기 워크플로를 따릅니다.

  3. Cloud Firestore 보안 규칙의 시작 모드를 선택합니다.

    테스트 모드

    모바일 및 웹 클라이언트 라이브러리를 시작할 때 유용하지만 모든 사용자가 데이터를 읽고 덮어쓸 수 있습니다. 테스트 완료 후 데이터 보안 섹션을 검토해야 합니다.

    웹, Apple 플랫폼 또는 Android SDK를 시작하려면 테스트 모드를 선택하세요.

    잠금 모드

    모바일 및 웹 클라이언트의 모든 읽기 및 쓰기를 거부합니다. 인증된 애플리케이션 서버(C#, Go, 자바, Node.js, PHP, Python, Ruby)에서는 사용자의 데이터베이스에 계속 액세스할 수 있습니다.

    C#, Go, 자바, Node.js, PHP, Python 또는 Ruby 서버 클라이언트 라이브러리를 시작하려면 잠금 모드를 선택하세요.

    Cloud Firestore 보안 규칙의 초기 집합이 기본 Cloud Firestore 데이터베이스에 적용됩니다. 프로젝트에 여러 데이터베이스를 만드는 경우 각 데이터베이스에 Cloud Firestore 보안 규칙을 배포할 수 있습니다.

  4. 데이터베이스의 위치를 선택합니다.

    • 이 위치 설정이 프로젝트의 기본 Google Cloud Platform(GCP) 리소스 위치입니다. 이 위치는 특히 기본 Cloud Storage 버킷 및 App Engine 앱(Cloud Scheduler 사용 시 필요)과 같이 위치 설정이 필요한 프로젝트의 GCP 서비스에 사용됩니다.

    • 위치를 선택할 수 없다면 프로젝트에 이미 기본 GCP 리소스 위치가 있는 것입니다. 이 위치는 프로젝트 생성 과정이나 위치 설정이 필요한 다른 서비스를 설정할 때 지정한 것입니다.

  5. 완료를 클릭합니다.

Cloud Firestore를 사용 설정하면 Cloud API Manager의 API도 사용 설정됩니다.

개발 환경 설정

필요한 종속 항목 및 클라이언트 라이브러리를 앱에 추가하세요.

웹 네임스페이스화된 API

  1. 안내에 따라 Firebase를 웹 앱에 추가합니다.
  2. Firebase 및 Cloud Firestore 라이브러리를 앱에 추가합니다.
    <script src="https://www.gstatic.com/firebasejs/10.7.2/firebase-app-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/10.7.2/firebase-firestore-compat.js"></script>
    Cloud Firestore SDK는 npm 패키지로도 제공됩니다.
    npm install firebase@10.7.2 --save
    
    Firebase와 Cloud Firestore를 수동으로 요청해야 합니다.
    import firebase from "firebase/compat/app";
    // Required for side-effects
    import "firebase/firestore";
    

웹 모듈식 API

  1. 안내에 따라 Firebase를 웹 앱에 추가합니다.
  2. Cloud Firestore SDK는 npm 패키지로 제공됩니다.
    npm install firebase@10.7.2 --save
    
    Firebase와 Cloud Firestore를 모두 가져와야 합니다.
    import { initializeApp } from "firebase/app";
    import { getFirestore } from "firebase/firestore";
    
iOS+

안내에 따라 Firebase를 Apple 앱에 추가합니다.

Swift Package Manager를 사용해 Firebase 종속 항목을 설치하고 관리하세요.

  1. 앱 프로젝트를 연 상태로 Xcode에서 File(파일) > Swift Packages(Swift 패키지) > Add Package Dependency(패키지 종속 항목 추가)로 이동합니다.
  2. 메시지가 표시되면 Firebase Apple 플랫폼 SDK 저장소를 추가합니다.
  3.   https://github.com/firebase/firebase-ios-sdk
      
  4. Firestore 라이브러리를 선택합니다.
  5. 완료되면 Xcode가 백그라운드에서 자동으로 종속 항목을 확인하고 다운로드하기 시작합니다.
Android
  1. 안내에 따라 Firebase를 Android 앱에 추가합니다.
  2. Firebase Android BoM을 사용하여 모듈(앱 수준) Gradle 파일(일반적으로 app/build.gradle.kts 또는 app/build.gradle)에서 Android용 Cloud Firestore 라이브러리의 종속 항목을 선언합니다.
    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.7.1"))
    
        // Declare the dependency for the Cloud Firestore library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-firestore")
    }
    

    Firebase Android BoM을 사용하면 앱에서 항상 호환되는 Firebase Android 라이브러리 버전을 사용합니다.

    (대안) BoM을 사용하지 않고 Firebase 라이브러리 종속 항목을 선언합니다.

    Firebase BoM을 사용하지 않도록 선택한 경우에는 종속 항목 줄에 각 Firebase 라이브러리 버전을 지정해야 합니다.

    앱에서 여러 Firebase 라이브러리를 사용하는 경우 모든 버전이 호환되도록 BoM을 사용하여 라이브러리 버전을 관리하는 것이 좋습니다.

    dependencies {
        // Declare the dependency for the Cloud Firestore library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-firestore:24.10.1")
    }
    

    Kotlin 전용 라이브러리 모듈을 찾고 계신가요? 2023년 10월 출시부터 Kotlin과 Java 개발자 모두 기본 라이브러리 모듈을 사용할 수 있습니다. 자세한 내용은 이니셔티브에 관한 FAQ를 참조하세요.

Dart

  1. 아직 구성하지 않았다면 Flutter 앱에서 Firebase를 구성하고 초기화합니다.
  2. Flutter 프로젝트의 루트에서 다음 명령어를 실행하여 플러그인을 설치합니다.
    flutter pub add cloud_firestore
  3. 완료되면 Flutter 애플리케이션을 다시 빌드합니다.
    flutter run
  4. 선택사항: 사전 컴파일된 프레임워크를 포함하여 iOS 및 macOS 빌드 시간을 개선합니다.

    현재 iOS용 Firestore SDK는 Xcode에서 빌드하는 데 5분 이상 걸릴 수 있는 코드를 사용합니다. 빌드 시간을 크게 줄이려면 Podfile의 target 'Runner' do 블록에 다음 줄을 추가하여 사전 컴파일된 버전을 사용하면 됩니다.

    target 'Runner' do
      use_frameworks!
      use_modular_headers!
    
      pod 'FirebaseFirestore',
        :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git',
        :tag => 'IOS_SDK_VERSION'
    
      flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
      target 'RunnerTests' do
        inherit! :search_paths
      end
    end

    IOS_SDK_VERSIONfirebase_corefirebase_sdk_version.rb 파일에 지정된 Firebase iOS SDK 버전으로 바꿉니다. firebase_core의 최신 버전을 사용하지 않는 경우 로컬 Pub 패키지 캐시(일반적으로 ~/.pub-cache)에서 이 파일을 찾습니다.

    또한 CocoaPods를 1.9.1 이상으로 업그레이드했는지 확인합니다.

    gem install cocoapods

    자세한 내용은 GitHub의 문제를 참조하세요.

자바
  1. Firebase Admin SDK를 앱에 추가합니다.
    • compile 'com.google.firebase:firebase-admin:1.32.0'
      
      Gradle 사용:
    • Maven 사용:
      <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>1.32.0</version>
      </dependency>
           
  2. 아래 안내에 따라 사용자 환경에서 적절한 사용자 인증 정보를 사용해 Cloud Firestore를 초기화합니다.
Python
  1. Firebase Admin SDK를 Python 앱에 추가합니다.
    pip install --upgrade firebase-admin
  2. 아래 안내에 따라 사용자 환경에서 적절한 사용자 인증 정보를 사용해 Cloud Firestore를 초기화합니다.
C++
  1. 안내에 따라 Firebase를 C++ 프로젝트에 추가합니다.
  2. Android용 C++ 인터페이스
    • Gradle 종속 항목 모듈(앱 수준) Gradle 파일(일반적으로 app/build.gradle)에 다음을 추가합니다.
              android.defaultConfig.externalNativeBuild.cmake {
                arguments "-DFIREBASE_CPP_SDK_DIR=$gradle.firebase_cpp_sdk_dir"
              }
      
              apply from: "$gradle.firebase_cpp_sdk_dir/Android/firebase_dependencies.gradle"
              firebaseCpp.dependencies {
                // earlier entries
                auth
                firestore
              }
              
    • 바이너리 종속 항목 마찬가지로 바이너리 종속 항목을 가져올 때 권장되는 방법은 CMakeLists.txt 파일에 다음을 추가하는 것입니다.
              add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
              set(firebase_libs firebase_auth firebase_firestore firebase_app)
              # Replace the target name below with the actual name of your target,
              # for example, "native-lib".
              target_link_libraries(${YOUR_TARGET_NAME_HERE} "${firebase_libs}")
              
  3. 데스크톱 통합을 설정하려면 C++ 프로젝트에 Firebase 추가를 참조하세요.
Unity
  1. 안내에 따라 Firebase를 Unity 프로젝트에 추가합니다.
  2. Unity 인터페이스를 사용하여 Android 빌드를 축소하도록 프로젝트를 구성합니다.
  3. Error while merging dex archives 메시지를 방지하려면 빌드를 축소해야 합니다.

    • 이 옵션은 Player Settings(플레이어 설정) > Android > Publishing Settings(게시 설정) > Minify(축소)에서 찾을 수 있습니다.
    • 옵션은 Unity 버전에 따라 다를 수 있으므로 공식 Unity 문서Firebase Unity 빌드 디버그 가이드를 참조하세요.
    • 축소를 사용 설정한 후에도 참조된 메서드의 개수가 여전히 한도를 초과하는 경우 또 다른 옵션은 다음 파일에서 multidex를 사용 설정하는 것입니다.
      • Player Settings(플레이어 설정)에서 Custom Gradle Template(커스텀 Gradle 템플릿)이 사용 설정된 경우 mainTemplate.gradle
      • 또는 Android 스튜디오를 사용하여 내보낸 프로젝트를 빌드하는 경우 모듈 수준 build.gradle 파일
Node.js
  1. Firebase Admin SDK를 앱에 추가합니다.
    npm install firebase-admin --save
  2. 아래 안내에 따라 사용자 환경에서 적절한 사용자 인증 정보를 사용해 Cloud Firestore를 초기화합니다.
Go
  1. Firebase Admin SDK를 Go 앱에 추가합니다.
    go get firebase.google.com/go
    
  2. 아래 안내에 따라 사용자 환경에서 적절한 사용자 인증 정보를 사용해 Cloud Firestore를 초기화합니다.
PHP
  1. Cloud Firestore 서버 클라이언트 라이브러리(자바, Node.js, Python, Go, PHP, C#, Ruby)는 인증을 위해 Google 애플리케이션 기본 사용자 인증 정보를 사용합니다.
    • 개발 환경에서 인증하려면 GOOGLE_APPLICATION_CREDENTIALS 환경 변수가 JSON 서비스 계정 키 파일을 가리키도록 설정합니다. API 콘솔 사용자 인증 정보 페이지에서 키 파일을 만들 수 있습니다.
      export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json"
    • 프로덕션 환경에서는 Cloud Firestore에 사용하는 것과 동일한 프로젝트를 사용하여 App Engine이나 Compute Engine에서 애플리케이션을 실행하는 경우 인증할 필요가 없습니다. 그 외의 경우는 서비스 계정을 설정합니다.
  2. 클라이언트 라이브러리를 사용하는 데 필요한 PHP용 gRPC 확장 프로그램을 설치하고 사용 설정합니다.
  3. Cloud Firestore PHP 라이브러리를 앱에 추가합니다.
    composer require google/cloud-firestore
C#
  1. Cloud Firestore 서버 클라이언트 라이브러리(자바, Node.js, Python, Go, PHP, C#, Ruby)는 인증을 위해 Google 애플리케이션 기본 사용자 인증 정보를 사용합니다.
    • 개발 환경에서 인증하려면 GOOGLE_APPLICATION_CREDENTIALS 환경 변수가 JSON 서비스 계정 키 파일을 가리키도록 설정합니다. API 콘솔 사용자 인증 정보 페이지에서 키 파일을 만들 수 있습니다.
      export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json"
    • 프로덕션 환경에서는 Cloud Firestore에 사용하는 것과 동일한 프로젝트를 사용하여 App Engine이나 Compute Engine에서 애플리케이션을 실행하는 경우 인증할 필요가 없습니다. 그 외의 경우는 서비스 계정을 설정합니다.
  2. Cloud Firestore C# 라이브러리를 .csproj 파일의 앱에 추가합니다.
    <ItemGroup>
      <PackageReference Include="Google.Cloud.Firestore" Version="1.1.0-beta01" />
    </ItemGroup>
  3. Program.cs 파일에 다음을 추가합니다.
    using Google.Cloud.Firestore;
Ruby
  1. Cloud Firestore 서버 클라이언트 라이브러리(자바, Node.js, Python, Go, PHP, C#, Ruby)는 인증을 위해 Google 애플리케이션 기본 사용자 인증 정보를 사용합니다.
    • 개발 환경에서 인증하려면 GOOGLE_APPLICATION_CREDENTIALS 환경 변수가 JSON 서비스 계정 키 파일을 가리키도록 설정합니다. API 콘솔 사용자 인증 정보 페이지에서 키 파일을 만들 수 있습니다.
      export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json"
    • 프로덕션 환경에서는 Cloud Firestore에 사용하는 것과 동일한 프로젝트를 사용하여 App Engine이나 Compute Engine에서 애플리케이션을 실행하는 경우 인증할 필요가 없습니다. 그 외의 경우는 서비스 계정을 설정합니다.
  2. Cloud Firestore Ruby 라이브러리를 Gemfile의 앱에 추가합니다.
    gem "google-cloud-firestore"
  3. 다음을 사용하여 Gemfile의 종속 항목을 설치합니다.
    bundle install

(선택사항) Firebase 로컬 에뮬레이터 도구 모음으로 프로토타입 제작 및 테스트

모바일 개발자를 대상으로 앱이 Cloud Firestore에서 데이터를 읽고 쓰는 방법에 대해 설명하기 전에 Cloud Firestore 기능을 프로토타입으로 제작하고 테스트하는 데 사용할 수 있는 도구 모음인 Firebase 로컬 에뮬레이터 도구 모음을 소개하려 합니다. 다양한 데이터 모델을 사용해 보거나, 보안 규칙을 최적화하거나, 백엔드와 상호작용할 수 있는 가장 비용 효율적인 방법을 찾는 경우 실시간 서비스를 배포하지 않고 로컬에서 작업할 수 있다는 것은 획기적인 아이디어입니다.

Cloud Firestore 에뮬레이터는 로컬 에뮬레이터 도구 모음에 포함되어 있으며 에뮬레이션된 데이터베이스 콘텐츠 및 구성은 물론 필요에 따라 에뮬레이션된 프로젝트 리소스(함수, 기타 데이터베이스, 보안 규칙)와 앱이 상호작용할 수 있게 해줍니다.

Cloud Firestore 에뮬레이터를 사용하려면 몇 가지 단계만 거치면 됩니다.

  1. 에뮬레이터에 연결하려면 앱의 테스트 구성에 코드 줄을 추가합니다.
  2. 로컬 프로젝트 디렉터리의 루트에서 firebase emulators:start를 실행합니다.
  3. 평소와 같이 Cloud Firestore 플랫폼 SDK를 사용하여 앱의 프로토타입 코드에서 호출합니다.

자세한 내용은 Cloud Firestore 및 Cloud Functions 관련 둘러보기를 참조하세요 로컬 에뮬레이터 도구 모음 소개도 살펴보세요.

Cloud Firestore 초기화

Cloud Firestore 인스턴스를 초기화합니다.

웹 모듈식 API

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://support.google.com/firebase/answer/7015592
const firebaseConfig = {
    FIREBASE_CONFIGURATION
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);


// Initialize Cloud Firestore and get a reference to the service
const db = getFirestore(app);

FIREBASE_CONFIGURATION을 웹 앱의 firebaseConfig로 바꿉니다.

기기의 연결이 끊겨도 데이터를 유지하려면 오프라인 데이터 사용 설정 문서를 참조하세요.

웹 네임스페이스화된 API

import firebase from "firebase/app";
import "firebase/firestore";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://support.google.com/firebase/answer/7015592
const firebaseConfig = {
    FIREBASE_CONFIGURATION
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);


// Initialize Cloud Firestore and get a reference to the service
const db = firebase.firestore();

FIREBASE_CONFIGURATION을 웹 앱의 firebaseConfig로 바꿉니다.

기기의 연결이 끊겨도 데이터를 유지하려면 오프라인 데이터 사용 설정 문서를 참조하세요.

Swift
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
import FirebaseCore
import FirebaseFirestore
FirebaseApp.configure()

let db = Firestore.firestore()
Objective-C
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
@import FirebaseCore;
@import FirebaseFirestore;

// Use Firebase library to configure APIs
[FIRApp configure];
  
FIRFirestore *defaultFirestore = [FIRFirestore firestore];

Kotlin+KTX

// Access a Cloud Firestore instance from your Activity
val db = Firebase.firestore

Java

// Access a Cloud Firestore instance from your Activity
FirebaseFirestore db = FirebaseFirestore.getInstance();

Dart

db = FirebaseFirestore.instance;
자바
Cloud Firestore SDK는 환경에 따라 다양한 방법으로 초기화됩니다. 가장 일반적인 방법은 아래와 같습니다. 전체 참조를 보려면 Admin SDK 초기화를 참조하세요.
  • Google Cloud에서 초기화
    import com.google.auth.oauth2.GoogleCredentials;
    import com.google.cloud.firestore.Firestore;
    
    import com.google.firebase.FirebaseApp;
    import com.google.firebase.FirebaseOptions;
    
    // Use the application default credentials
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(credentials)
        .setProjectId(projectId)
        .build();
    FirebaseApp.initializeApp(options);
    
    Firestore db = FirestoreClient.getFirestore();
    
  • 자체 서버에서 초기화

    자체 서버에서 Firebase Admin SDK를 사용하려면 서비스 계정을 사용합니다.

    Google Cloud 콘솔에서 IAM 및 관리자 > 서비스 계정으로 이동합니다. 새 비공개 키를 생성하고 JSON 파일을 저장합니다. 그런 다음 이 파일을 사용하여 SDK를 초기화합니다.

    import com.google.auth.oauth2.GoogleCredentials;
    import com.google.cloud.firestore.Firestore;
    
    import com.google.firebase.FirebaseApp;
    import com.google.firebase.FirebaseOptions;
    
    // Use a service account
    InputStream serviceAccount = new FileInputStream("path/to/serviceAccount.json");
    GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(credentials)
        .build();
    FirebaseApp.initializeApp(options);
    
    Firestore db = FirestoreClient.getFirestore();
    
  • Python
    Cloud Firestore SDK는 환경에 따라 다양한 방법으로 초기화됩니다. 가장 일반적인 방법은 아래와 같습니다. 전체 참조를 보려면 Admin SDK 초기화를 참조하세요.
  • Google Cloud에서 초기화
    import firebase_admin
    from firebase_admin import firestore
    
    # Application Default credentials are automatically created.
    app = firebase_admin.initialize_app()
    db = firestore.client()

    기존 애플리케이션 기본 사용자 인증 정보를 사용하여 SDK를 초기화할 수도 있습니다.

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore
    
    # Use the application default credentials.
    cred = credentials.ApplicationDefault()
    
    firebase_admin.initialize_app(cred)
    db = firestore.client()
  • 자체 서버에서 초기화

    자체 서버에서 Firebase Admin SDK를 사용하려면 서비스 계정을 사용합니다.

    Google Cloud 콘솔에서 IAM 및 관리자 > 서비스 계정으로 이동합니다. 새 비공개 키를 생성하고 JSON 파일을 저장합니다. 그런 다음 이 파일을 사용하여 SDK를 초기화합니다.

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore
    
    # Use a service account.
    cred = credentials.Certificate('path/to/serviceAccount.json')
    
    app = firebase_admin.initialize_app(cred)
    
    db = firestore.client()
  • Python

    Cloud Firestore SDK는 환경에 따라 다양한 방법으로 초기화됩니다. 가장 일반적인 방법은 아래와 같습니다. 전체 참조를 보려면 Admin SDK 초기화를 참조하세요.
  • Google Cloud에서 초기화
    import firebase_admin
    from firebase_admin import firestore_async
    
    # Application Default credentials are automatically created.
    app = firebase_admin.initialize_app()
    db = firestore_async.client()

    기존 애플리케이션 기본 사용자 인증 정보를 사용하여 SDK를 초기화할 수도 있습니다.

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore_async
    
    # Use the application default credentials.
    cred = credentials.ApplicationDefault()
    
    firebase_admin.initialize_app(cred)
    db = firestore_async.client()
  • 자체 서버에서 초기화

    자체 서버에서 Firebase Admin SDK를 사용하려면 서비스 계정을 사용합니다.

    Google Cloud 콘솔에서 IAM 및 관리자 > 서비스 계정으로 이동합니다. 새 비공개 키를 생성하고 JSON 파일을 저장합니다. 그런 다음 이 파일을 사용하여 SDK를 초기화합니다.

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore_async
    
    # Use a service account.
    cred = credentials.Certificate('path/to/serviceAccount.json')
    
    app = firebase_admin.initialize_app(cred)
    
    db = firestore_async.client()
  • C++
    // Make sure the call to `Create()` happens some time before you call Firestore::GetInstance().
    App::Create();
    Firestore* db = Firestore::GetInstance();
    Node.js
    Cloud Firestore SDK는 환경에 따라 다양한 방법으로 초기화됩니다. 가장 일반적인 방법은 아래와 같습니다. 전체 참조를 보려면 Admin SDK 초기화를 참조하세요.
    • Cloud Functions에서 초기화
      const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
      const { getFirestore, Timestamp, FieldValue, Filter } = require('firebase-admin/firestore');
      initializeApp();
      
      const db = getFirestore();
      
    • Google Cloud에서 초기화
      const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
      const { getFirestore, Timestamp, FieldValue, Filter } = require('firebase-admin/firestore');
      initializeApp({
        credential: applicationDefault()
      });
      
      const db = getFirestore();
    • 자체 서버에서 초기화

      자체 서버(또는 다른 Node.js 환경)에서 Firebase Admin SDK를 사용하려면 서비스 계정을 사용합니다. Google Cloud 콘솔에서 IAM 및 관리자 > 서비스 계정으로 이동합니다. 새 비공개 키를 생성하고 JSON 파일을 저장합니다. 그런 다음 이 파일을 사용하여 SDK를 초기화합니다.

      const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
      const { getFirestore, Timestamp, FieldValue, Filter } = require('firebase-admin/firestore');
      const serviceAccount = require('./path/to/serviceAccountKey.json');
      
      initializeApp({
        credential: cert(serviceAccount)
      });
      
      const db = getFirestore();
      
    Go
    Cloud Firestore SDK는 환경에 따라 다양한 방법으로 초기화됩니다. 가장 일반적인 방법은 아래와 같습니다. 전체 참조를 보려면 Admin SDK 초기화를 참조하세요.
  • Google Cloud에서 초기화
    import (
      "log"
    
      firebase "firebase.google.com/go"
      "google.golang.org/api/option"
    )
    
    // Use the application default credentials
    ctx := context.Background()
    conf := &firebase.Config{ProjectID: projectID}
    app, err := firebase.NewApp(ctx, conf)
    if err != nil {
      log.Fatalln(err)
    }
    
    client, err := app.Firestore(ctx)
    if err != nil {
      log.Fatalln(err)
    }
    defer client.Close()
    
  • 자체 서버에서 초기화

    자체 서버에서 Firebase Admin SDK를 사용하려면 서비스 계정을 사용합니다.

    Google Cloud 콘솔에서 IAM 및 관리자 > 서비스 계정으로 이동합니다. 새 비공개 키를 생성하고 JSON 파일을 저장합니다. 그런 다음 이 파일을 사용하여 SDK를 초기화합니다.

    import (
      "log"
    
      firebase "firebase.google.com/go"
      "google.golang.org/api/option"
    )
    
    // Use a service account
    ctx := context.Background()
    sa := option.WithCredentialsFile("path/to/serviceAccount.json")
    app, err := firebase.NewApp(ctx, nil, sa)
    if err != nil {
      log.Fatalln(err)
    }
    
    client, err := app.Firestore(ctx)
    if err != nil {
      log.Fatalln(err)
    }
    defer client.Close()
    
  • PHP

    PHP

    Cloud Firestore 클라이언트를 설치하고 만드는 방법에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리를 참조하세요.

    use Google\Cloud\Firestore\FirestoreClient;
    
    /**
     * Initialize Cloud Firestore with default project ID.
     */
    function setup_client_create(string $projectId = null)
    {
        // Create the Cloud Firestore client
        if (empty($projectId)) {
            // The `projectId` parameter is optional and represents which project the
            // client will act on behalf of. If not supplied, the client falls back to
            // the default project inferred from the environment.
            $db = new FirestoreClient();
            printf('Created Cloud Firestore client with default project ID.' . PHP_EOL);
        } else {
            $db = new FirestoreClient([
                'projectId' => $projectId,
            ]);
            printf('Created Cloud Firestore client with project ID: %s' . PHP_EOL, $projectId);
        }
    }
    Unity
    using Firebase.Firestore;
    using Firebase.Extensions;
    FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
    C#

    C#

    Cloud Firestore 클라이언트를 설치하고 만드는 방법에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리를 참조하세요.

    FirestoreDb db = FirestoreDb.Create(project);
    Console.WriteLine("Created Cloud Firestore client with project ID: {0}", project);
    Ruby
    require "google/cloud/firestore"
    
    # The `project_id` parameter is optional and represents which project the
    # client will act on behalf of. If not supplied, the client falls back to the
    # default project inferred from the environment.
    firestore = Google::Cloud::Firestore.new project_id: project_id
    
    puts "Created Cloud Firestore client with given project ID."

    데이터 추가

    Cloud Firestore는 컬렉션에 저장되는 문서에 데이터를 저장합니다. 문서에 데이터를 처음 추가할 때 Cloud Firestore에서 암시적으로 컬렉션과 문서를 만듭니다. 컬렉션이나 문서를 명시적으로 만들 필요가 없습니다.

    다음 예시 코드를 사용해 새 컬렉션과 문서를 만듭니다.

    웹 모듈식 API

    import { collection, addDoc } from "firebase/firestore"; 
    
    try {
      const docRef = await addDoc(collection(db, "users"), {
        first: "Ada",
        last: "Lovelace",
        born: 1815
      });
      console.log("Document written with ID: ", docRef.id);
    } catch (e) {
      console.error("Error adding document: ", e);
    }

    웹 네임스페이스화된 API

    db.collection("users").add({
        first: "Ada",
        last: "Lovelace",
        born: 1815
    })
    .then((docRef) => {
        console.log("Document written with ID: ", docRef.id);
    })
    .catch((error) => {
        console.error("Error adding document: ", error);
    });
    Swift
    참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
    // Add a new document with a generated ID
    do {
      let ref = try await db.collection("users").addDocument(data: [
        "first": "Ada",
        "last": "Lovelace",
        "born": 1815
      ])
      print("Document added with ID: \(ref.documentID)")
    } catch {
      print("Error adding document: \(error)")
    }
    Objective-C
    참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
    // Add a new document with a generated ID
    __block FIRDocumentReference *ref =
        [[self.db collectionWithPath:@"users"] addDocumentWithData:@{
          @"first": @"Ada",
          @"last": @"Lovelace",
          @"born": @1815
        } completion:^(NSError * _Nullable error) {
          if (error != nil) {
            NSLog(@"Error adding document: %@", error);
          } else {
            NSLog(@"Document added with ID: %@", ref.documentID);
          }
        }];

    Kotlin+KTX

    // Create a new user with a first and last name
    val user = hashMapOf(
        "first" to "Ada",
        "last" to "Lovelace",
        "born" to 1815,
    )
    
    // Add a new document with a generated ID
    db.collection("users")
        .add(user)
        .addOnSuccessListener { documentReference ->
            Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}")
        }
        .addOnFailureListener { e ->
            Log.w(TAG, "Error adding document", e)
        }

    Java

    // Create a new user with a first and last name
    Map<String, Object> user = new HashMap<>();
    user.put("first", "Ada");
    user.put("last", "Lovelace");
    user.put("born", 1815);
    
    // Add a new document with a generated ID
    db.collection("users")
            .add(user)
            .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                @Override
                public void onSuccess(DocumentReference documentReference) {
                    Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "Error adding document", e);
                }
            });

    Dart

    // Create a new user with a first and last name
    final user = <String, dynamic>{
      "first": "Ada",
      "last": "Lovelace",
      "born": 1815
    };
    
    // Add a new document with a generated ID
    db.collection("users").add(user).then((DocumentReference doc) =>
        print('DocumentSnapshot added with ID: ${doc.id}'));
    Java
    DocumentReference docRef = db.collection("users").document("alovelace");
    // Add document data  with id "alovelace" using a hashmap
    Map<String, Object> data = new HashMap<>();
    data.put("first", "Ada");
    data.put("last", "Lovelace");
    data.put("born", 1815);
    //asynchronously write data
    ApiFuture<WriteResult> result = docRef.set(data);
    // ...
    // result.get() blocks on response
    System.out.println("Update time : " + result.get().getUpdateTime());
    Python
    doc_ref = db.collection("users").document("alovelace")
    doc_ref.set({"first": "Ada", "last": "Lovelace", "born": 1815})

    Python

    doc_ref = db.collection("users").document("alovelace")
    await doc_ref.set({"first": "Ada", "last": "Lovelace", "born": 1815})
    C++
    // Add a new document with a generated ID
    Future<DocumentReference> user_ref =
        db->Collection("users").Add({{"first", FieldValue::String("Ada")},
                                     {"last", FieldValue::String("Lovelace")},
                                     {"born", FieldValue::Integer(1815)}});
    
    user_ref.OnCompletion([](const Future<DocumentReference>& future) {
      if (future.error() == Error::kErrorOk) {
        std::cout << "DocumentSnapshot added with ID: " << future.result()->id()
                  << std::endl;
      } else {
        std::cout << "Error adding document: " << future.error_message() << std::endl;
      }
    });
    Node.js
    const docRef = db.collection('users').doc('alovelace');
    
    await docRef.set({
      first: 'Ada',
      last: 'Lovelace',
      born: 1815
    });
    Go
    _, _, err := client.Collection("users").Add(ctx, map[string]interface{}{
    	"first": "Ada",
    	"last":  "Lovelace",
    	"born":  1815,
    })
    if err != nil {
    	log.Fatalf("Failed adding alovelace: %v", err)
    }
    PHP

    PHP

    Cloud Firestore 클라이언트를 설치하고 만드는 방법에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리를 참조하세요.

    $docRef = $db->collection('samples/php/users')->document('alovelace');
    $docRef->set([
        'first' => 'Ada',
        'last' => 'Lovelace',
        'born' => 1815
    ]);
    printf('Added data to the lovelace document in the users collection.' . PHP_EOL);
    Unity
    DocumentReference docRef = db.Collection("users").Document("alovelace");
    Dictionary<string, object> user = new Dictionary<string, object>
    {
    	{ "First", "Ada" },
    	{ "Last", "Lovelace" },
    	{ "Born", 1815 },
    };
    docRef.SetAsync(user).ContinueWithOnMainThread(task => {
    	Debug.Log("Added data to the alovelace document in the users collection.");
    });
    C#
    DocumentReference docRef = db.Collection("users").Document("alovelace");
    Dictionary<string, object> user = new Dictionary<string, object>
    {
        { "First", "Ada" },
        { "Last", "Lovelace" },
        { "Born", 1815 }
    };
    await docRef.SetAsync(user);
    Ruby
    doc_ref = firestore.doc "#{collection_path}/alovelace"
    
    doc_ref.set(
      {
        first: "Ada",
        last:  "Lovelace",
        born:  1815
      }
    )
    
    puts "Added data to the alovelace document in the users collection."

    이제 users 컬렉션에 다른 문서를 추가합니다. 첫 번째 문서에는 나타나지 않는 키-값 쌍(중간 이름)이 문서에 포함된다는 점에 유의하세요. 컬렉션의 문서에는 다른 정보 집합이 포함될 수 있습니다.

    웹 모듈식 API

    // Add a second document with a generated ID.
    import { addDoc, collection } from "firebase/firestore"; 
    
    try {
      const docRef = await addDoc(collection(db, "users"), {
        first: "Alan",
        middle: "Mathison",
        last: "Turing",
        born: 1912
      });
    
      console.log("Document written with ID: ", docRef.id);
    } catch (e) {
      console.error("Error adding document: ", e);
    }

    웹 네임스페이스화된 API

    // Add a second document with a generated ID.
    db.collection("users").add({
        first: "Alan",
        middle: "Mathison",
        last: "Turing",
        born: 1912
    })
    .then((docRef) => {
        console.log("Document written with ID: ", docRef.id);
    })
    .catch((error) => {
        console.error("Error adding document: ", error);
    });
    Swift
    참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
    // Add a second document with a generated ID.
    do {
      let ref = try await db.collection("users").addDocument(data: [
        "first": "Alan",
        "middle": "Mathison",
        "last": "Turing",
        "born": 1912
      ])
      print("Document added with ID: \(ref.documentID)")
    } catch {
      print("Error adding document: \(error)")
    }
    Objective-C
    참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
    // Add a second document with a generated ID.
    __block FIRDocumentReference *ref =
        [[self.db collectionWithPath:@"users"] addDocumentWithData:@{
          @"first": @"Alan",
          @"middle": @"Mathison",
          @"last": @"Turing",
          @"born": @1912
        } completion:^(NSError * _Nullable error) {
          if (error != nil) {
            NSLog(@"Error adding document: %@", error);
          } else {
            NSLog(@"Document added with ID: %@", ref.documentID);
          }
        }];

    Kotlin+KTX

    // Create a new user with a first, middle, and last name
    val user = hashMapOf(
        "first" to "Alan",
        "middle" to "Mathison",
        "last" to "Turing",
        "born" to 1912,
    )
    
    // Add a new document with a generated ID
    db.collection("users")
        .add(user)
        .addOnSuccessListener { documentReference ->
            Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}")
        }
        .addOnFailureListener { e ->
            Log.w(TAG, "Error adding document", e)
        }

    Java

    // Create a new user with a first, middle, and last name
    Map<String, Object> user = new HashMap<>();
    user.put("first", "Alan");
    user.put("middle", "Mathison");
    user.put("last", "Turing");
    user.put("born", 1912);
    
    // Add a new document with a generated ID
    db.collection("users")
            .add(user)
            .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                @Override
                public void onSuccess(DocumentReference documentReference) {
                    Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "Error adding document", e);
                }
            });

    Dart

    // Create a new user with a first and last name
    final user = <String, dynamic>{
      "first": "Alan",
      "middle": "Mathison",
      "last": "Turing",
      "born": 1912
    };
    
    // Add a new document with a generated ID
    db.collection("users").add(user).then((DocumentReference doc) =>
        print('DocumentSnapshot added with ID: ${doc.id}'));
    Java
    DocumentReference docRef = db.collection("users").document("aturing");
    // Add document data with an additional field ("middle")
    Map<String, Object> data = new HashMap<>();
    data.put("first", "Alan");
    data.put("middle", "Mathison");
    data.put("last", "Turing");
    data.put("born", 1912);
    
    ApiFuture<WriteResult> result = docRef.set(data);
    System.out.println("Update time : " + result.get().getUpdateTime());
    Python
    doc_ref = db.collection("users").document("aturing")
    doc_ref.set({"first": "Alan", "middle": "Mathison", "last": "Turing", "born": 1912})

    Python

    doc_ref = db.collection("users").document("aturing")
    await doc_ref.set(
        {"first": "Alan", "middle": "Mathison", "last": "Turing", "born": 1912}
    )
    C++
    db->Collection("users")
        .Add({{"first", FieldValue::String("Alan")},
              {"middle", FieldValue::String("Mathison")},
              {"last", FieldValue::String("Turing")},
              {"born", FieldValue::Integer(1912)}})
        .OnCompletion([](const Future<DocumentReference>& future) {
          if (future.error() == Error::kErrorOk) {
            std::cout << "DocumentSnapshot added with ID: "
                      << future.result()->id() << std::endl;
          } else {
            std::cout << "Error adding document: " << future.error_message()
                      << std::endl;
          }
        });
    Node.js
    const aTuringRef = db.collection('users').doc('aturing');
    
    await aTuringRef.set({
      'first': 'Alan',
      'middle': 'Mathison',
      'last': 'Turing',
      'born': 1912
    });
    Go
    _, _, err = client.Collection("users").Add(ctx, map[string]interface{}{
    	"first":  "Alan",
    	"middle": "Mathison",
    	"last":   "Turing",
    	"born":   1912,
    })
    if err != nil {
    	log.Fatalf("Failed adding aturing: %v", err)
    }
    PHP

    PHP

    Cloud Firestore 클라이언트를 설치하고 만드는 방법에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리를 참조하세요.

    $docRef = $db->collection('samples/php/users')->document('aturing');
    $docRef->set([
        'first' => 'Alan',
        'middle' => 'Mathison',
        'last' => 'Turing',
        'born' => 1912
    ]);
    printf('Added data to the aturing document in the users collection.' . PHP_EOL);
    Unity
    DocumentReference docRef = db.Collection("users").Document("aturing");
    Dictionary<string, object> user = new Dictionary<string, object>
    {
    	{ "First", "Alan" },
    	{ "Middle", "Mathison" },
    	{ "Last", "Turing" },
    	{ "Born", 1912 }
    };
    docRef.SetAsync(user).ContinueWithOnMainThread(task => {
    	Debug.Log("Added data to the aturing document in the users collection.");
    });
    C#
    DocumentReference docRef = db.Collection("users").Document("aturing");
    Dictionary<string, object> user = new Dictionary<string, object>
    {
        { "First", "Alan" },
        { "Middle", "Mathison" },
        { "Last", "Turing" },
        { "Born", 1912 }
    };
    await docRef.SetAsync(user);
    Ruby
    doc_ref = firestore.doc "#{collection_path}/aturing"
    
    doc_ref.set(
      {
        first:  "Alan",
        middle: "Mathison",
        last:   "Turing",
        born:   1912
      }
    )
    
    puts "Added data to the aturing document in the users collection."

    데이터 읽기

    Firebase Console의 데이터 뷰어를 사용하여 Cloud Firestore에 추가한 데이터를 빠르게 확인합니다.

    'get' 메서드를 사용해 전체 컬렉션을 가져올 수도 있습니다.

    웹 모듈식 API

    import { collection, getDocs } from "firebase/firestore"; 
    
    const querySnapshot = await getDocs(collection(db, "users"));
    querySnapshot.forEach((doc) => {
      console.log(`${doc.id} => ${doc.data()}`);
    });

    웹 네임스페이스화된 API

    db.collection("users").get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            console.log(`${doc.id} => ${doc.data()}`);
        });
    });
    Swift
    참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
    do {
      let snapshot = try await db.collection("users").getDocuments()
      for document in snapshot.documents {
        print("\(document.documentID) => \(document.data())")
      }
    } catch {
      print("Error getting documents: \(error)")
    }
    Objective-C
    참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
    [[self.db collectionWithPath:@"users"]
        getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot,
                                     NSError * _Nullable error) {
          if (error != nil) {
            NSLog(@"Error getting documents: %@", error);
          } else {
            for (FIRDocumentSnapshot *document in snapshot.documents) {
              NSLog(@"%@ => %@", document.documentID, document.data);
            }
          }
        }];

    Kotlin+KTX

    db.collection("users")
        .get()
        .addOnSuccessListener { result ->
            for (document in result) {
                Log.d(TAG, "${document.id} => ${document.data}")
            }
        }
        .addOnFailureListener { exception ->
            Log.w(TAG, "Error getting documents.", exception)
        }

    Java

    db.collection("users")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d(TAG, document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.w(TAG, "Error getting documents.", task.getException());
                    }
                }
            });

    Dart

    await db.collection("users").get().then((event) {
      for (var doc in event.docs) {
        print("${doc.id} => ${doc.data()}");
      }
    });
    Java
    // asynchronously retrieve all users
    ApiFuture<QuerySnapshot> query = db.collection("users").get();
    // ...
    // query.get() blocks on response
    QuerySnapshot querySnapshot = query.get();
    List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
    for (QueryDocumentSnapshot document : documents) {
      System.out.println("User: " + document.getId());
      System.out.println("First: " + document.getString("first"));
      if (document.contains("middle")) {
        System.out.println("Middle: " + document.getString("middle"));
      }
      System.out.println("Last: " + document.getString("last"));
      System.out.println("Born: " + document.getLong("born"));
    }
    Python
    users_ref = db.collection("users")
    docs = users_ref.stream()
    
    for doc in docs:
        print(f"{doc.id} => {doc.to_dict()}")

    Python

    users_ref = db.collection("users")
    docs = users_ref.stream()
    
    async for doc in docs:
        print(f"{doc.id} => {doc.to_dict()}")
    C++
    Future<QuerySnapshot> users = db->Collection("users").Get();
    users.OnCompletion([](const Future<QuerySnapshot>& future) {
      if (future.error() == Error::kErrorOk) {
        for (const DocumentSnapshot& document : future.result()->documents()) {
          std::cout << document << std::endl;
        }
      } else {
        std::cout << "Error getting documents: " << future.error_message()
                  << std::endl;
      }
    });
    Node.js
    const snapshot = await db.collection('users').get();
    snapshot.forEach((doc) => {
      console.log(doc.id, '=>', doc.data());
    });
    Go
    iter := client.Collection("users").Documents(ctx)
    for {
    	doc, err := iter.Next()
    	if err == iterator.Done {
    		break
    	}
    	if err != nil {
    		log.Fatalf("Failed to iterate: %v", err)
    	}
    	fmt.Println(doc.Data())
    }
    PHP

    PHP

    Cloud Firestore 클라이언트를 설치하고 만드는 방법에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리를 참조하세요.

    $usersRef = $db->collection('samples/php/users');
    $snapshot = $usersRef->documents();
    foreach ($snapshot as $user) {
        printf('User: %s' . PHP_EOL, $user->id());
        printf('First: %s' . PHP_EOL, $user['first']);
        if (!empty($user['middle'])) {
            printf('Middle: %s' . PHP_EOL, $user['middle']);
        }
        printf('Last: %s' . PHP_EOL, $user['last']);
        printf('Born: %d' . PHP_EOL, $user['born']);
        printf(PHP_EOL);
    }
    printf('Retrieved and printed out all documents from the users collection.' . PHP_EOL);
    Unity
    CollectionReference usersRef = db.Collection("users");
    usersRef.GetSnapshotAsync().ContinueWithOnMainThread(task =>
    {
      QuerySnapshot snapshot = task.Result;
      foreach (DocumentSnapshot document in snapshot.Documents)
      {
        Debug.Log(String.Format("User: {0}", document.Id));
        Dictionary<string, object> documentDictionary = document.ToDictionary();
        Debug.Log(String.Format("First: {0}", documentDictionary["First"]));
        if (documentDictionary.ContainsKey("Middle"))
        {
          Debug.Log(String.Format("Middle: {0}", documentDictionary["Middle"]));
        }
    
        Debug.Log(String.Format("Last: {0}", documentDictionary["Last"]));
        Debug.Log(String.Format("Born: {0}", documentDictionary["Born"]));
      }
    
      Debug.Log("Read all data from the users collection.");
    });
    C#
    CollectionReference usersRef = db.Collection("users");
    QuerySnapshot snapshot = await usersRef.GetSnapshotAsync();
    foreach (DocumentSnapshot document in snapshot.Documents)
    {
        Console.WriteLine("User: {0}", document.Id);
        Dictionary<string, object> documentDictionary = document.ToDictionary();
        Console.WriteLine("First: {0}", documentDictionary["First"]);
        if (documentDictionary.ContainsKey("Middle"))
        {
            Console.WriteLine("Middle: {0}", documentDictionary["Middle"]);
        }
        Console.WriteLine("Last: {0}", documentDictionary["Last"]);
        Console.WriteLine("Born: {0}", documentDictionary["Born"]);
        Console.WriteLine();
    }
    Ruby
    users_ref = firestore.col collection_path
    users_ref.get do |user|
      puts "#{user.document_id} data: #{user.data}."
    end

    데이터 보안

    웹, Android 또는 Apple 플랫폼 SDK를 사용하는 경우 Firebase 인증Cloud Firestore 보안 규칙을 사용하여 Cloud Firestore의 데이터에 보안을 적용합니다.

    다음은 시작하는 데 사용할 수 있는 기본 규칙 세트입니다. Console의 규칙 탭에서 보안 규칙을 수정할 수 있습니다.

    인증 필요

    // Allow read/write access on all documents to any user signed in to the application
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth != null;
        }
      }
    }
    

    잠금 모드

    // Deny read/write access to all users under any conditions
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if false;
        }
      }
    }
    

    테스트 모드

    // Allow read/write access to all users under any conditions
    // Warning: **NEVER** use this rule set in production; it allows
    // anyone to overwrite your entire database.
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    웹, Android 또는 iOS 앱을 프로덕션에 배포하기 전에 앱 클라이언트만 Cloud Firestore 데이터에 액세스할 수 있도록 하는 단계를 수행합니다. 앱 체크 문서를 참조하세요.

    서버 SDK 중 하나를 사용하는 경우 Identity and Access Management(IAM)를 사용하여 Cloud Firestore의 데이터를 보호합니다.

    동영상 튜토리얼 보기

    Cloud Firestore 모바일 클라이언트 라이브러리 시작에 대한 자세한 안내는 다음 동영상 튜토리얼을 참조하세요.

    iOS+
    Android

    그 밖에도 많은 동영상을 Firebase YouTube 채널에서 확인할 수 있습니다.

    다음 단계

    다음 주제를 자세히 알아보세요.

    • Codelab - Android, iOS 또는 용 Codelab의 안내에 따라 실제 앱에서 Cloud Firestore를 사용하는 방법을 알아보세요.
    • 데이터 모델 - 계층적 데이터 및 하위 컬렉션을 포함하여 Cloud Firestore의 데이터 구조를 자세히 알아보세요.
    • 데이터 추가 - Cloud Firestore에서 데이터를 만들고 업데이트하는 방법을 자세히 알아보세요.
    • 데이터 가져오기 - 데이터를 검색하는 방법을 자세히 알아보세요.
    • 단순 쿼리 및 복합 쿼리 실행 - 단순 쿼리 및 복합 쿼리 실행 방법을 알아보세요.
    • 쿼리 순서 지정 및 제한 - 쿼리에서 반환된 데이터의 순서를 지정하고 제한하는 방법을 알아보세요.