इस क्विकस्टार्ट गाइड में, Firestore Enterprise वर्शन को सेट अप करने और डेटा जोड़ने का तरीका बताया गया है. इसके बाद, Firebase कंसोल में जोड़े गए डेटा के लिए क्वेरी करने के लिए, कोर ऑपरेशन या पाइपलाइन ऑपरेशन का इस्तेमाल करने का तरीका बताया गया है.
Cloud Firestore, मोबाइल या वेब SDK टूल और सर्वर क्लाइंट लाइब्रेरी के साथ काम करता है:
Cloud Firestore, Android, iOS, वेब वगैरह के लिए एसडीके टूल के साथ काम करता है. Cloud Firestore Security Rules और Firebase Authentication के साथ मिलकर, मोबाइल और वेब एसडीके, सर्वरलेस ऐप्लिकेशन आर्किटेक्चर के साथ काम करते हैं. इनमें क्लाइंट सीधे आपके Cloud Firestore डेटाबेस से कनेक्ट होते हैं.
Cloud Firestore, Java, Node.js, और Python के लिए सर्वर क्लाइंट लाइब्रेरी के साथ काम करता है. इन क्लाइंट लाइब्रेरी का इस्तेमाल करके, अपने डेटाबेस के पूरे ऐक्सेस के साथ खास अधिकार वाले सर्वर एनवायरमेंट सेट अप करें. इन लाइब्रेरी के बारे में ज़्यादा जानने के लिए, सर्वर क्लाइंट लाइब्रेरी के लिए क्विकस्टार्ट पर जाएं.
एआई असिस्टेंस के लिए एआई प्रॉम्प्ट
एआई असिस्टेंस पाने के लिए, यहां दिए गए प्रॉम्प्ट को अपने एआई कोडिंग एजेंट (जैसे, 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 **Enterprise edition**. Help me to choose the best location for my database instance.
Firestore Enterprise वर्शन का डेटाबेस बनाना
अगर आपने पहले से कोई Firebase प्रोजेक्ट नहीं बनाया है, तो कोई Firebase प्रोजेक्ट बनाएं: Firebase कंसोल में, प्रोजेक्ट जोड़ें पर क्लिक करें. इसके बाद, Firebase प्रोजेक्ट बनाने या किसी मौजूदा Google Cloud प्रोजेक्ट में Firebase की सेवाएं जोड़ने के लिए, स्क्रीन पर दिए गए निर्देशों का पालन करें.
बाईं ओर मौजूद पैनल में, Databases & Storage > Firestore पर जाएं.
डेटाबेस बनाएं पर क्लिक करें.
डेटाबेस मोड के लिए, Enterprise चुनें.
ऑपरेशन मोड के लिए, Firestore in Native Mode को चुनें. यह मोड, कोर और पाइपलाइन ऑपरेशन के साथ काम करता है.
अपने डेटाबेस के लिए कोई जगह चुनें.
अपने Cloud Firestore Security Rules के लिए, शुरुआती मोड चुनें:
- टेस्ट मोड
यह मोबाइल और वेब क्लाइंट लाइब्रेरी का इस्तेमाल शुरू करने के लिए अच्छा है. हालांकि, इससे कोई भी व्यक्ति आपके डेटा को पढ़ सकता है और उसे ओवरराइट कर सकता है. जांच करने के बाद, अपना डेटा सुरक्षित करें सेक्शन को ज़रूर देखें.
वेब, Apple प्लैटफ़ॉर्म या Android SDK का इस्तेमाल शुरू करने के लिए, टेस्ट मोड चुनें.
- प्रोडक्शन मोड
मोबाइल और वेब क्लाइंट से, डेटा को पढ़ने और उसमें बदलाव करने की अनुमति नहीं देता. पुष्टि किए गए आपके ऐप्लिकेशन सर्वर (Python) अब भी आपके डेटाबेस को ऐक्सेस कर सकते हैं.
Cloud Firestore Security Rules का आपका शुरुआती सेट, आपके डिफ़ॉल्ट Cloud Firestore डेटाबेस पर लागू होगा. अगर आपने अपने प्रोजेक्ट के लिए कई डेटाबेस बनाए हैं, तो हर डेटाबेस के लिए Cloud Firestore Security Rules को डिप्लॉय किया जा सकता है.
बनाएं पर क्लिक करें.
Firestore Enterprise वर्शन को चालू करने पर, Cloud API Manager में भी एपीआई चालू हो जाता है.
अपना डेवलपमेंट एनवायरमेंट सेट अप करना
अपने ऐप्लिकेशन में ज़रूरी डिपेंडेंसी और क्लाइंट लाइब्रेरी जोड़ें.
Web
- अपने वेब ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
-
Cloud Firestore SDK टूल, npm पैकेज के तौर पर उपलब्ध है.
आपको Firebase और Cloud Firestore, दोनों को इंपोर्ट करना होगा.npm install firebase@13.0.0 --save
import { initializeApp } from "firebase/app"; import { getFirestore } from "firebase/firestore";
iOS+
अपने Apple ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift Package Manager का इस्तेमाल करें.
- Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलें. इसके बाद, File > Swift Packages > Add Package Dependency पर जाएं.
- प्रॉम्प्ट मिलने पर, Firebase Apple प्लैटफ़ॉर्म SDK टूल की रिपॉज़िटरी जोड़ें:
- Firestore लाइब्रेरी चुनें.
- इसके बाद, Xcode बैकग्राउंड में आपकी डिपेंडेंसी से जुड़ी समस्या को हल करना और उन्हें डाउनलोड करना शुरू कर देगा.
https://github.com/firebase/firebase-ios-sdk
Android
- अपने Android ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
- 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, दोनों के डेवलपर मुख्य लाइब्रेरी मॉड्यूल पर निर्भर हो सकते हैं. ज़्यादा जानकारी के लिए, इस पहल के बारे में अक्सर पूछे जाने वाले सवाल देखें.
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); // When initializing Firestore, remember to use the name of the database you created earlier: const db = initializeFirestore(app, {}, 'your-new-enterprise-database');
FIREBASE_CONFIGURATION को अपने वेब ऐप्लिकेशन के
firebaseConfig से बदलें.
डिवाइस का कनेक्शन बंद होने पर डेटा को बनाए रखने के लिए, ऑफ़लाइन डेटा चालू करें दस्तावेज़ देखें.
Swift
import FirebaseCore import FirebaseFirestore FirebaseApp.configure() // When initializing Firestore, remember to use the name of the database you created earlier: let db = Firestore.firestore(database: "your-new-enterprise-database")
Kotlin
// Access a Cloud Firestore instance from your Activity // When initializing Firestore, remember to use the name of the database you created earlier: val firestore = FirebaseFirestore.getInstance("your-new-enterprise-database")
Java
// Access a Cloud Firestore instance from your Activity // When initializing Firestore, remember to use the name of the database you created earlier: FirebaseFirestore firestore = FirebaseFirestore.getInstance("your-new-enterprise-database");
मुख्य कार्रवाइयों का इस्तेमाल करके डेटा जोड़ना
डेटा के बारे में क्वेरी करने के लिए, कोर ऑपरेशन और पाइपलाइन ऑपरेशन को एक्सप्लोर करें. इसके लिए, कोर ऑपरेशन का इस्तेमाल करके अपने डेटाबेस में डेटा जोड़ें.
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
// 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)") }
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); } });
अब 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
// 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)") }
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); } });
मुख्य कार्रवाइयों का इस्तेमाल करके डेटा पढ़ना
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
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)") }
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()); } } });
पाइपलाइन ऑपरेशन का इस्तेमाल करके डेटा पढ़ना
अब पाइपलाइन क्वेरी के अनुभव की तुलना, कोर क्वेरी के अनुभव से की जा सकती है.
Web
// The import/require of "firebase/firestore/pipelines" has a side-effect // of extending the Firestore class with the `.pipeline()` method. // Without this import/require, you will not be able to create a Pipeline. // import { execute } from "firebase/firestore/pipelines"; const readDataPipeline = db.pipeline() .collection("users"); // Execute the pipeline and handle the result try { const querySnapshot = await execute(readDataPipeline); querySnapshot.results.forEach((result) => { console.log(`${result.id} => ${result.data()}`); }); } catch (error) { console.error("Error getting documents: ", error); }
Swift
do { // Initialize a Firestore Pipeline instance and specify the "users" collection as the // input stage. let snapshot = try await db.pipeline() .collection("users") .execute() // Execute the pipeline to retrieve documents. // Iterate through the documents in the pipeline results, similar to a regular query // snapshot. for result in snapshot.results { print("\(result.id ?? "no ID") => \(result.data)") } } catch { print("Error getting documents with pipeline: \(error)") }
Kotlin
val readDataPipeline = db.pipeline() .collection("users") // Execute the pipeline and handle the result readDataPipeline.execute() .addOnSuccessListener { result -> for (document in result) { println("${document.getId()} => ${document.getData()}") } } .addOnFailureListener { exception -> println("Error getting documents: $exception") }
Java
Pipeline readDataPipeline = db.pipeline() .collection("users"); readDataPipeline.execute() .addOnSuccessListener(new OnSuccessListener<Pipeline.Snapshot>() { @Override public void onSuccess(Pipeline.Snapshot snapshot) { for (PipelineResult result : snapshot.getResults()) { System.out.println(result.getId() + " => " + result.getData()); } } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { System.out.println("Error getting documents: " + e); } });
मोबाइल और वेब एसडीके के लिए अपना डेटा सुरक्षित करना
अगर वेब, 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) का इस्तेमाल करें.
अगले चरण
इन विषयों की मदद से, कोर और पाइपलाइन ऑपरेशंस के बारे में ज़्यादा जानें:
- मुख्य कार्रवाइयों की मदद से क्वेरी करने के बारे में ज़्यादा जानें
- पाइपलाइन ऑपरेशन की मदद से क्वेरी करने के बारे में ज़्यादा जानें.
- टास्क को ज़्यादा असरदार तरीक़े से पूरा करने के लिए, एआई डेवलपमेंट टूल को बेहतर बनाने का तरीक़ा जानें. साथ ही, Firebase एजेंट स्किल के साथ Cloud Firestore सबसे सही तरीक़े (जैसे, Cloud Firestore Security Rules लिखना) इस्तेमाल करने का तरीक़ा जानें.