Firestore Standard edition का इस्तेमाल शुरू करना

इस क्विकस्टार्ट में, Cloud Firestore को सेट अप करने, डेटा जोड़ने, और Firebase कंसोल में जोड़े गए डेटा को देखने का तरीका बताया गया है.

Cloud Firestore, मोबाइल या वेब SDK टूल और सर्वर क्लाइंट लाइब्रेरी के साथ काम करता है:

  • Cloud Firestore, Android, iOS, वेब वगैरह के लिए एसडीके टूल के साथ काम करता है. Cloud Firestore Security Rules और Firebase Authentication के साथ मिलकर, मोबाइल और वेब एसडीके, सर्वरलेस ऐप्लिकेशन आर्किटेक्चर के साथ काम करते हैं. इनमें क्लाइंट सीधे आपके Cloud Firestore डेटाबेस से कनेक्ट होते हैं.

  • Cloud Firestore, C#, Go, Java, Node.js, PHP, Python, और Ruby के लिए सर्वर क्लाइंट लाइब्रेरी के साथ काम करता है. इन क्लाइंट लाइब्रेरी का इस्तेमाल करके, अपने डेटाबेस के पूरे ऐक्सेस के साथ खास अधिकार वाले सर्वर एनवायरमेंट सेट अप करें. इन लाइब्रेरी के बारे में ज़्यादा जानने के लिए, सर्वर क्लाइंट लाइब्रेरी के लिए क्विकस्टार्ट पर जाएं.

एआई असिस्टेंस पाने के लिए, यहां दिए गए प्रॉम्प्ट को अपने एआई कोडिंग एजेंट (जैसे, Android Studio में Gemini, Antigravity, Claude Code, Codex, और Cursor) में कॉपी करें. प्रॉम्प्ट को अपनी पसंद के मुताबिक बनाया जा सकता है. साथ ही, एजेंट के साथ मिलकर उसमें बदलाव किया जा सकता है. इंस्टॉल करने के बारे में ज़्यादा जानकारी के लिए, Firebase एजेंट स्किल पर जाएं.

Help me use Firebase in my app.
1. Make sure that **Firebase agent skills** are installed and updated. For Claude Code, Codex, or Cursor agents, help me install the Firebase plugin. For other agents, run `npx skills add firebase/agent-skills`. Always try to use **Firebase agent skills** for Firebase-related tasks.
2. Help me set up and use Firebase for the backend of my app. Set up Firestore database in **Standard edition**. Help me to choose the best location for my database instance.

Cloud Firestore डेटाबेस बनाएं

  1. अगर आपने पहले से कोई Firebase प्रोजेक्ट नहीं बनाया है, तो कोई Firebase प्रोजेक्ट बनाएं: Firebase कंसोल में, प्रोजेक्ट जोड़ें पर क्लिक करें. इसके बाद, Firebase प्रोजेक्ट बनाने या किसी मौजूदा Google Cloud प्रोजेक्ट में Firebase की सेवाएं जोड़ने के लिए, स्क्रीन पर दिए गए निर्देशों का पालन करें.

  2. बाईं ओर मौजूद पैनल में, Databases & Storage > Firestore पर जाएं.

  3. डेटाबेस बनाएं पर क्लिक करें.

  4. अपने डेटाबेस के लिए कोई जगह चुनें.

    अगर आपको कोई जगह चुनने का विकल्प नहीं मिल रहा है, तो इसका मतलब है कि आपके प्रोजेक्ट के लिए, "डिफ़ॉल्ट Google Cloud संसाधनों के लिए जगह" पहले से सेट है. आपके प्रोजेक्ट के कुछ संसाधन (जैसे, डिफ़ॉल्ट Cloud Firestore इंस्टेंस) एक ही जगह की जानकारी पर निर्भर करते हैं. इनकी जगह की जानकारी, प्रोजेक्ट बनाते समय या इस जगह की जानकारी पर निर्भर करने वाली किसी दूसरी सेवा को सेट अप करते समय सेट की जा सकती है.

  5. अपने Cloud Firestore Security Rules के लिए, शुरुआती मोड चुनें:

    टेस्ट मोड

    यह मोबाइल और वेब क्लाइंट लाइब्रेरी का इस्तेमाल शुरू करने के लिए अच्छा है. हालांकि, इससे कोई भी व्यक्ति आपके डेटा को पढ़ सकता है और उसे ओवरराइट कर सकता है. जांच करने के बाद, अपना डेटा सुरक्षित करें सेक्शन को ज़रूर देखें.

    वेब, Apple प्लैटफ़ॉर्म या Android SDK टूल का इस्तेमाल शुरू करने के लिए, टेस्ट मोड चुनें.

    प्रोडक्शन मोड

    मोबाइल और वेब क्लाइंट से, डेटा को पढ़ने और उसमें बदलाव करने की अनुमति नहीं देता. पुष्टि किए गए आपके ऐप्लिकेशन सर्वर (C#, Go, Java, Node.js, PHP, Python या Ruby) अब भी आपके डेटाबेस को ऐक्सेस कर सकते हैं.

    C#, Go, Java, Node.js, PHP, Python या Ruby सर्वर क्लाइंट लाइब्रेरी का इस्तेमाल शुरू करने के लिए, प्रोडक्शन मोड चुनें.

    Cloud Firestore Security Rules का आपका शुरुआती सेट, आपके डिफ़ॉल्ट Cloud Firestore डेटाबेस पर लागू होगा. अगर आपने अपने प्रोजेक्ट के लिए कई डेटाबेस बनाए हैं, तो हर डेटाबेस के लिए Cloud Firestore Security Rules को डिप्लॉय किया जा सकता है.

  6. बनाएं पर क्लिक करें.

Cloud Firestore को चालू करने पर, Cloud API Manager में भी एपीआई चालू हो जाता है.

अपना डेवलपमेंट एनवायरमेंट सेट अप करना

अपने ऐप्लिकेशन में ज़रूरी डिपेंडेंसी और क्लाइंट लाइब्रेरी जोड़ें.

Web

  1. अपने वेब ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
  2. Cloud Firestore SDK टूल, npm पैकेज के तौर पर उपलब्ध है.
    npm install firebase@13.0.0 --save
    आपको Firebase और Cloud Firestore, दोनों को इंपोर्ट करना होगा.
    import { initializeApp } from "firebase/app";
    import { getFirestore } from "firebase/firestore";

Web

  1. अपने वेब ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
  2. अपने ऐप्लिकेशन में Firebase और Cloud Firestore लाइब्रेरी जोड़ें:
    <script src="https://www.gstatic.com/firebasejs/13.0.0/firebase-app-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/13.0.0/firebase-firestore-compat.js"></script>
    Cloud Firestore SDK, npm पैकेज के तौर पर भी उपलब्ध है.
    npm install firebase@13.0.0 --save
    आपको Firebase और Cloud Firestore, दोनों को मैन्युअल तरीके से जोड़ना होगा.
    import firebase from "firebase/compat/app";
    // Required for side-effects
    import "firebase/firestore";
iOS+

अपने Apple ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.

Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift Package Manager का इस्तेमाल करें.

  1. Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलें. इसके बाद, File > Swift Packages > Add Package Dependency पर जाएं.
  2. प्रॉम्प्ट मिलने पर, Firebase Apple प्लैटफ़ॉर्म SDK टूल की रिपॉज़िटरी जोड़ें:
  3.   https://github.com/firebase/firebase-ios-sdk
      
  4. Firestore लाइब्रेरी चुनें.
  5. इसके बाद, Xcode बैकग्राउंड में आपकी डिपेंडेंसी से जुड़ी समस्या को हल करना और उन्हें डाउनलोड करना शुरू कर देगा.
Android
  1. अपने Android ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
  2. Firebase Android बीओएम का इस्तेमाल करके, मॉड्यूल (ऐप्लिकेशन-लेवल) की 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:35.0.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 बीओएम का इस्तेमाल करने पर, आपका ऐप्लिकेशन हमेशा 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:27.0.0")
    }

    क्या आपको Kotlin के लिए खास लाइब्रेरी मॉड्यूल चाहिए? अक्टूबर 2023 की रिलीज़ से, Kotlin और Java, दोनों के डेवलपर मुख्य लाइब्रेरी मॉड्यूल पर निर्भर हो सकते हैं. ज़्यादा जानकारी के लिए, इस पहल के बारे में अक्सर पूछे जाने वाले सवाल देखें.

Dart

  1. अगर आपने अब तक ऐसा नहीं किया है, तो अपने Flutter ऐप्लिकेशन में Firebase को कॉन्फ़िगर और शुरू करें.
  2. प्लग इन इंस्टॉल करने के लिए, अपने Flutter प्रोजेक्ट के रूट से यह कमांड चलाएं:
    flutter pub add cloud_firestore
  3. इसके बाद, अपने Flutter ऐप्लिकेशन को फिर से बनाएं:
    flutter run
  4. ज़रूरी नहीं: पहले से कंपाइल किए गए फ़्रेमवर्क को शामिल करके, iOS और macOS के बिल्ड टाइम को बेहतर बनाएं.

    फ़िलहाल, iOS के लिए Firestore SDK टूल, ऐसे कोड पर निर्भर करता है जिसे Xcode में बनाने में पांच मिनट से ज़्यादा समय लग सकता है. बिल्ड टाइम को काफ़ी कम करने के लिए, पहले से कंपाइल किए गए वर्शन का इस्तेमाल किया जा सकता है. इसके लिए, अपनी 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_VERSION को Firebase iOS SDK के उस वर्शन से बदलें जो firebase_core की firebase_sdk_version.rb फ़ाइल में बताया गया है. अगर आपके पास firebase_core का नया वर्शन नहीं है, तो इस फ़ाइल को अपने लोकल Pub पैकेज कैश (आम तौर पर ~/.pub-cache) में ढूंढें.

    इसके अलावा, पक्का करें कि आपने CocoaPods को 1.9.1 या इसके बाद के वर्शन पर अपग्रेड किया हो:

    gem install cocoapods

    ज़्यादा जानकारी के लिए, GitHub पर समस्या देखें.

C++
  1. अपने C++ प्रोजेक्ट में Firebase जोड़ने के लिए, दिए गए निर्देशों का पालन करें.
  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. अपने Unity प्रोजेक्ट में Firebase जोड़ने के लिए, दिए गए निर्देशों का पालन करें.
  2. Android बिल्ड को छोटा करने के लिए, Unity इंटरफ़ेस का इस्तेमाल करके अपने प्रोजेक्ट को कॉन्फ़िगर करें.
  3. Error while merging dex archives मैसेज से बचने के लिए, आपको बिल्ड को छोटा करना होगा.

    • यह विकल्प, Player Settings > Android > Publishing Settings > Minify में जाकर देखा जा सकता है.
    • Unity के अलग-अलग वर्शन में विकल्प अलग-अलग हो सकते हैं. इसलिए, आधिकारिक Unity दस्तावेज़ और Firebase Unity बिल्ड डीबग गाइड देखें.
    • अगर काट-छांट की सुविधा चालू करने के बाद भी, रेफ़रंस किए गए तरीकों की संख्या तय सीमा से ज़्यादा है, तो multidex को चालू करने का एक और विकल्प है:
      • mainTemplate.gradle, अगर प्लेयर सेटिंग में जाकर, कस्टम Gradle टेंप्लेट चालू किया गया है
      • अगर एक्सपोर्ट किए गए प्रोजेक्ट को बनाने के लिए Android Studio का इस्तेमाल किया जाता है, तो मॉड्यूल-लेवल की build.gradle फ़ाइल में जोड़ें.

(ज़रूरी नहीं) Firebase Local Emulator Suite की मदद से प्रोटोटाइप बनाना और उसकी जांच करना

मोबाइल डेवलपर के लिए, यह बताने से पहले कि आपका ऐप्लिकेशन Cloud Firestore में डेटा कैसे लिखता है और कैसे पढ़ता है, आइए हम आपको टूल के एक सेट के बारे में बताते हैं. इसका इस्तेमाल करके, Cloud Firestore की सुविधाओं का प्रोटोटाइप बनाया जा सकता है और उन्हें टेस्ट किया जा सकता है: Firebase Local Emulator Suite. अगर आपको अलग-अलग डेटा मॉडल आज़माने हैं, सुरक्षा नियमों को ऑप्टिमाइज़ करना है या बैक-एंड के साथ इंटरैक्ट करने का सबसे किफ़ायती तरीका ढूंढना है, तो लाइव सेवाओं को डिप्लॉय किए बिना लोकल लेवल पर काम करना एक अच्छा आइडिया हो सकता है.

Cloud Firestore एम्युलेटर, Local Emulator Suite का हिस्सा है. इसकी मदद से, आपका ऐप्लिकेशन आपके एम्युलेट किए गए डेटाबेस कॉन्टेंट और कॉन्फ़िगरेशन के साथ इंटरैक्ट कर सकता है. साथ ही, आपके एम्युलेट किए गए प्रोजेक्ट रिसॉर्स (फ़ंक्शन, अन्य डेटाबेस, और सुरक्षा के नियम) के साथ भी इंटरैक्ट कर सकता है.

Cloud Firestore एम्युलेटर का इस्तेमाल करने के लिए, बस कुछ ही चरणों को पूरा करना होता है:

  1. एम्युलेटर से कनेक्ट करने के लिए, अपने ऐप्लिकेशन के टेस्ट कॉन्फ़िगरेशन में कोड की एक लाइन जोड़ना.
  2. अपनी लोकल प्रोजेक्ट डायरेक्ट्री के रूट से, firebase emulators:start चलाएं.
  3. हमेशा की तरह, Cloud Firestore प्लैटफ़ॉर्म SDK टूल का इस्तेमाल करके, अपने ऐप्लिकेशन के प्रोटोटाइप कोड से कॉल करना.

Cloud Firestore और Cloud Functions के बारे में ज़्यादा जानकारी देने वाला वॉकथ्रू उपलब्ध है. आपको Local Emulator Suite के बारे में जानकारी भी देखनी चाहिए.

Cloud Firestore को शुरू करें

Cloud Firestore का कोई इंस्टेंस शुरू करें:

Web

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 से बदलें.

डिवाइस का कनेक्शन बंद होने पर डेटा को बनाए रखने के लिए, ऑफ़लाइन डेटा चालू करें दस्तावेज़ देखें.

Web

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 और App Clip टारगेट पर उपलब्ध नहीं है.
import FirebaseCore
import FirebaseFirestore
FirebaseApp.configure()

let db = Firestore.firestore()
Objective-C
ध्यान दें: यह प्रॉडक्ट, watchOS और App Clip टारगेट पर उपलब्ध नहीं है.
@import FirebaseCore;
@import FirebaseFirestore;

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

Kotlin

// 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;
C++
// Make sure the call to `Create()` happens some time before you call Firestore::GetInstance().
App::Create();
Firestore* db = Firestore::GetInstance();
Unity
using Firebase.Firestore;
using Firebase.Extensions;
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;

डेटा जोड़ें

Cloud Firestore, डेटा को दस्तावेज़ों में सेव करता है. ये दस्तावेज़, कलेक्शन में सेव किए जाते हैं. Cloud Firestore, दस्तावेज़ में पहली बार डेटा जोड़ने पर, कलेक्शन और दस्तावेज़ों को अपने-आप बनाता है. आपको साफ़ तौर पर कलेक्शन या दस्तावेज़ बनाने की ज़रूरत नहीं है.

यहां दिए गए उदाहरण कोड का इस्तेमाल करके, नया कलेक्शन और दस्तावेज़ बनाएं.

Web

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);
}

Web

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 और App Clip टारगेट पर उपलब्ध नहीं है.
// 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 और App Clip टारगेट पर उपलब्ध नहीं है.
// 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

// 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}'));
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;
  }
});
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.");
});

अब users कलेक्शन में कोई दूसरा दस्तावेज़ जोड़ें. ध्यान दें कि इस दस्तावेज़ में एक ऐसा की-वैल्यू पेयर (मिडिल नेम) शामिल है जो पहले दस्तावेज़ में नहीं दिखता. किसी कलेक्शन में मौजूद दस्तावेज़ों में, अलग-अलग तरह की जानकारी हो सकती है.

Web

// 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);
}

Web

// 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 और App Clip टारगेट पर उपलब्ध नहीं है.
// 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 और App Clip टारगेट पर उपलब्ध नहीं है.
// 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

// 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}'));
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;
      }
    });
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.");
});

डेटा पढ़ने की अनुमति दें

Firebase कंसोल में डेटा व्यूअर का इस्तेमाल करके, तुरंत पुष्टि करें कि आपने Cloud Firestore में डेटा जोड़ा है.

पूरे कलेक्शन को वापस पाने के लिए, "get" तरीके का भी इस्तेमाल किया जा सकता है.

Web

import { collection, getDocs } from "firebase/firestore"; 

const querySnapshot = await getDocs(collection(db, "users"));
querySnapshot.forEach((doc) => {
  console.log(doc.id, "=>", doc.data());
});

Web

db.collection("users").get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(doc.id, "=>", doc.data());
    });
});
Swift
ध्यान दें: यह प्रॉडक्ट, watchOS और App Clip टारगेट पर उपलब्ध नहीं है.
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 और App Clip टारगेट पर उपलब्ध नहीं है.
[[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

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()}");
  }
});
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;
  }
});
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.");
});

अपना डेटा सुरक्षित करें

अगर वेब, Android या Apple प्लैटफ़ॉर्म के SDK टूल का इस्तेमाल किया जा रहा है, तो Cloud Firestore में अपने डेटा को सुरक्षित रखने के लिए, Firebase से पुष्टि करना और Cloud Firestore Security Rules का इस्तेमाल करें.

यहां कुछ बुनियादी नियम सेट दिए गए हैं. इनका इस्तेमाल करके, शुरुआत की जा सकती है. सुरक्षा के नियमों में बदलाव करने के लिए, Firebase कंसोल में डेटाबेस और स्टोरेज > Firestore > नियम टैब पर जाएं.

पुष्टि करना ज़रूरी है

// Allow read/write access to a document keyed by the user's UID
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{uid} {
      allow read, write: if request.auth != null && request.auth.uid == uid;
    }
  }
}

प्रोडक्शन मोड

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

अपने वेब, Android या iOS ऐप्लिकेशन को प्रोडक्शन में डिप्लॉय करने से पहले, यह पक्का करें कि सिर्फ़ आपके ऐप्लिकेशन क्लाइंट ही Cloud Firestore डेटा को ऐक्सेस कर सकें. App Check से जुड़ा दस्तावेज़ देखें.

अगर सर्वर एसडीके में से किसी एक का इस्तेमाल किया जा रहा है, तो Cloud Firestore में अपने डेटा को सुरक्षित रखने के लिए, पहचान और ऐक्सेस मैनेजमेंट (IAM) का इस्तेमाल करें.

वीडियो ट्यूटोरियल देखना

Cloud Firestore मोबाइल क्लाइंट लाइब्रेरी का इस्तेमाल शुरू करने के बारे में ज़्यादा जानकारी पाने के लिए, इनमें से कोई एक वीडियो ट्यूटोरियल देखें:

वेब
iOS+
Android

Firebase के YouTube चैनल पर आपको ज़्यादा वीडियो मिल सकते हैं.

अगले चरण

इन विषयों के बारे में ज़्यादा जानकारी पाएं:

  • कोडलैब — Android, iOS या वेब के लिए कोडलैब का इस्तेमाल करके, किसी असली ऐप्लिकेशन में Cloud Firestore को इस्तेमाल करने का तरीका जानें.
  • डेटा मॉडल — इस बारे में ज़्यादा जानें कि Cloud Firestore में डेटा को कैसे स्ट्रक्चर किया जाता है. इसमें हैरारकी वाला डेटा और सब-कलेक्शन शामिल हैं.
  • डेटा जोड़ना — Cloud Firestore में डेटा बनाने और उसे अपडेट करने के बारे में ज़्यादा जानें.
  • डेटा पाना — डेटा वापस पाने के तरीके के बारे में ज़्यादा जानें.
  • आसान और मुश्किल क्वेरी करना — आसान और मुश्किल क्वेरी करने का तरीका जानें.
  • क्वेरी को क्रम से लगाना और सीमित करना अपनी क्वेरी से मिले डेटा को क्रम से लगाने और सीमित करने का तरीका जानें.
  • Firebase एजेंट स्किल — एआई डेवलपमेंट टूल को बेहतर बनाने का तरीका जानें, ताकि टास्क ज़्यादा असरदार तरीके से पूरे किए जा सकें. साथ ही, Cloud Firestore के सबसे सही तरीके इस्तेमाल करने का तरीका जानें. जैसे, Cloud Firestore Security Rules लिखना.