इस क्विकस्टार्ट में आपको Cloud Firestore सेट अप करने, डेटा जोड़ने, और Firebase कंसोल में जोड़े गए डेटा को देखने का तरीका बताया गया है.
Cloud Firestore डेटाबेस बनाना
अगर आपने पहले से Firebase प्रोजेक्ट नहीं बनाया है, तो: Firebase कंसोल में प्रोजेक्ट जोड़ें पर क्लिक करें. इसके बाद, Firebase प्रोजेक्ट बनाने या मौजूदा GCP प्रोजेक्ट में Firebase सेवाएं जोड़ने के लिए, स्क्रीन पर दिए गए निर्देशों का पालन करें.
Firebase कंसोल के Cloud Firestore सेक्शन पर जाएं. आपसे कोई मौजूदा Firebase प्रोजेक्ट चुनने के लिए कहा जाएगा. डेटाबेस बनाने के वर्कफ़्लो को फ़ॉलो करें.
अपने Cloud Firestore के सुरक्षा नियमों के लिए, कोई शुरुआती मोड चुनें:
- टेस्ट मोड
यह मोबाइल और वेब क्लाइंट लाइब्रेरी के साथ शुरुआत करने के लिए अच्छा है, लेकिन इनसे कोई भी आपका डेटा पढ़ और ओवरराइट कर सकता है. जांच करने के बाद, अपना डेटा सुरक्षित करें सेक्शन को ज़रूर देखें.
वेब, Apple प्लैटफ़ॉर्म या Android SDK का इस्तेमाल शुरू करने के लिए, टेस्ट मोड चुनें.
- लॉक मोड
मोबाइल और वेब क्लाइंट के किसी भी तरह के पढ़े और लिखे गए कॉन्टेंट को अस्वीकार करता है. आपके पुष्टि किए गए ऐप्लिकेशन सर्वर (C#, Go, Java, Node.js, PHP, Python या Ruby) अब भी आपके डेटाबेस को ऐक्सेस कर सकते हैं.
C#, Go, Java, Node.js, PHP, Python या Ruby सर्वर क्लाइंट लाइब्रेरी का इस्तेमाल शुरू करने के लिए, लॉक मोड को चुनें.
Cloud Firestore के सुरक्षा नियमों का आपका शुरुआती सेट, आपके डिफ़ॉल्ट Cloud Firestore डेटाबेस पर लागू होगा. अगर आप अपने प्रोजेक्ट के लिए एक से ज़्यादा डेटाबेस बनाते हैं, तो हर डेटाबेस के लिए Cloud Firestore के सुरक्षा नियम लागू किए जा सकते हैं.
अपने डेटाबेस के लिए कोई जगह चुनें.
जगह की जानकारी की यह सेटिंग आपके प्रोजेक्ट की डिफ़ॉल्ट Google Cloud Platform (GCP) संसाधन जगह है. ध्यान दें कि जगह की इस जानकारी का इस्तेमाल आपके प्रोजेक्ट में उन GCP सेवाओं के लिए किया जाएगा जिनके लिए जगह की जानकारी की सेटिंग की ज़रूरत है. खास तौर पर, आपका डिफ़ॉल्ट Cloud Storage बकेट और आपका App Engine ऐप्लिकेशन (अगर Cloud Scheduler इस्तेमाल किया जाता है, तो यह ज़रूरी होता है).
अगर जगह चुनने में समस्या आ रही है, तो इसका मतलब है कि आपके प्रोजेक्ट में पहले से ही डिफ़ॉल्ट GCP रिसॉर्स लोकेशन मौजूद है. इसे प्रोजेक्ट बनाते समय या ऐसी कोई दूसरी सेवा सेट अप करते समय सेट किया गया था जिसके लिए जगह की जानकारी की सेटिंग की ज़रूरत होती है.
हो गया पर क्लिक करें.
जब आप Cloud Firestore को चालू करते हैं, तो यह Cloud API मैनेजर में एपीआई को भी चालू कर देता है.
अपना डेवलपमेंट एनवायरमेंट सेट अप करें
अपने ऐप्लिकेशन में ज़रूरी डिपेंडेंसी और क्लाइंट लाइब्रेरी जोड़ें.
वेब नेमस्पेसेड एपीआई
- Firebase को अपने वेब ऐप्लिकेशन में जोड़ने के लिए, निर्देशों का पालन करें.
- अपने ऐप्लिकेशन में Firebase और Cloud Firestore लाइब्रेरी जोड़ें:
<script src="https://www.gstatic.com/firebasejs/10.12.2/firebase-app-compat.js"></script> <script src="https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore-compat.js"></script>
Cloud Firestore SDK टूल, एनपीएम पैकेज के तौर पर भी उपलब्ध है.npm install firebase@10.12.2 --save
आपको Firebase और Cloud Firestore, दोनों को मैन्युअल तरीके से सेट अप करना होगा.import firebase from "firebase/compat/app"; // Required for side-effects import "firebase/firestore";
वेब मॉड्यूलर एपीआई
- Firebase को अपने वेब ऐप्लिकेशन में जोड़ने के लिए, निर्देशों का पालन करें.
-
Cloud Firestore SDK टूल, एनपीएम पैकेज के तौर पर उपलब्ध है.
npm install firebase@10.12.2 --save
आपको Firebase और Cloud Firestore, दोनों इंपोर्ट करने होंगे.import { initializeApp } from "firebase/app"; import { getFirestore } from "firebase/firestore";
iOS और उसके बाद के वर्शन
अपने Apple ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift पैकेज मैनेजर का इस्तेमाल करें.
- Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलने के बाद, फ़ाइल > Swift Packages > पैकेज डिपेंडेंसी जोड़ें पर जाएं.
- जब कहा जाए, तब Firebase Apple प्लैटफ़ॉर्म SDK टूल का रिपॉज़िटरी जोड़ें:
- Firestore लाइब्रेरी चुनें.
- यह काम पूरा होने के बाद, Xcode बैकग्राउंड में आपकी डिपेंडेंसी को अपने-आप रिज़ॉल्व और डाउनलोड करना शुरू कर देगा.
https://github.com/firebase/firebase-ios-sdk
Android
- Firebase को अपने Android ऐप्लिकेशन में जोड़ने के लिए, निर्देशों का पालन करें.
- 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:33.1.0")) // 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:25.0.0") }
Kotlin की मदद से खास लाइब्रेरी मॉड्यूल की तलाश कर रहे हैं? अक्टूबर 2023 की रिलीज़ से, Kotlin और Java डेवलपर, दोनों मुख्य लाइब्रेरी मॉड्यूल पर निर्भर हो सकते हैं (ज़्यादा जानकारी के लिए, इस पहल के बारे में अक्सर पूछे जाने वाले सवाल देखें).
Dart
- अगर आपने अभी तक Firebase को कॉन्फ़िगर नहीं किया है, तो अपने Flutter ऐप्लिकेशन में Firebase को कॉन्फ़िगर करें और उसे शुरू करें.
- अपने Flutter प्रोजेक्ट के रूट से, प्लगिन को इंस्टॉल करने के लिए
यह कमांड चलाएं:
flutter pub add cloud_firestore
- यह प्रोसेस पूरी होने के बाद, अपना Flutter ऐप्लिकेशन फिर से बनाएं:
flutter run
- ज़रूरी नहीं: पहले से कंपाइल किए गए फ़्रेमवर्क को शामिल करके, 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_core
कीfirebase_sdk_version.rb
फ़ाइल में दिए गए Firebase iOS SDK टूल के वर्शन से बदलें. अगर आपके पासfirebase_core
का नया वर्शन नहीं है, तो इस फ़ाइल को अपने लोकल Pub पैकेज की कैश मेमोरी (आम तौर पर~/.pub-cache
) में ढूंढें.इसके अलावा, पक्का करें कि आपने CocoaPods को 1.9.1 या इसके बाद के वर्शन पर अपग्रेड कर लिया हो:
gem install cocoapods
ज़्यादा जानकारी के लिए, GitHub से जुड़ी समस्या देखें.
Java
- अपने ऐप्लिकेशन में Firebase एडमिन SDK टूल जोड़ें:
-
Gradle का इस्तेमाल करना:
compile 'com.google.firebase:firebase-admin:1.32.0'
-
Maven का इस्तेमाल करना:
<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>1.32.0</version> </dependency>
-
Gradle का इस्तेमाल करना:
- अपने एनवायरमेंट में सही क्रेडेंशियल के साथ Cloud Firestore शुरू करने के लिए, नीचे दिए गए निर्देशों का पालन करें.
Python
- अपने Python ऐप्लिकेशन में Firebase एडमिन SDK टूल जोड़ें:
pip install --upgrade firebase-admin
- अपने एनवायरमेंट में सही क्रेडेंशियल के साथ Cloud Firestore शुरू करने के लिए, नीचे दिए गए निर्देशों का पालन करें.
C++
- Firebase को अपने C++ प्रोजेक्ट में जोड़ने के लिए, निर्देशों का पालन करें.
- 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}")
- डेस्कटॉप इंटिग्रेशन सेट अप करने के लिए, Firebase को अपने C++ प्रोजेक्ट में जोड़ना लेख देखें.
Unity
- Firebase को अपने Unity प्रोजेक्ट में जोड़ने के लिए, निर्देशों का पालन करें.
- Android बिल्ड को छोटा करने के लिए, अपना प्रोजेक्ट कॉन्फ़िगर करने के लिए Unity इंटरफ़ेस का इस्तेमाल करें.
- यह विकल्प प्लेयर सेटिंग > Android > पब्लिश करने की सेटिंग > छोटा करें में देखा जा सकता है.
- Unity के अलग-अलग वर्शन में विकल्प अलग-अलग हो सकते हैं. इसलिए, आधिकारिक Unity दस्तावेज़ और Firebase Unity बिल्ड डीबग गाइड देखें.
-
अगर छोटा करने की सुविधा चालू करने के बाद भी, रेफ़रंस के लिए दिए गए तरीकों की संख्या
तय सीमा से ज़्यादा हो जाती है, तो दूसरे विकल्प के तौर पर
multidex
को चालू किया जा सकता है:-
प्लेयर सेटिंग में कस्टम ग्रेडल टेंप्लेट चालू होने पर,
mainTemplate.gradle
-
या, अगर एक्सपोर्ट किए गए प्रोजेक्ट को बनाने के लिए Android Studio का इस्तेमाल किया जाता है, तो मॉड्यूल-लेवल की
build.gradle
फ़ाइल का इस्तेमाल करें.
-
प्लेयर सेटिंग में कस्टम ग्रेडल टेंप्लेट चालू होने पर,
Error while merging dex archives
मैसेज से बचने के लिए, आपको बिल्ड को छोटा करना होगा.
Node.js के लिए
-
अपने ऐप्लिकेशन में Firebase एडमिन SDK टूल जोड़ें:
npm install firebase-admin --save
- अपने एनवायरमेंट में सही क्रेडेंशियल के साथ Cloud Firestore शुरू करने के लिए, नीचे दिए गए निर्देशों का पालन करें.
शुरू करें
- अपने Go ऐप्लिकेशन में Firebase एडमिन SDK जोड़ें:
go get firebase.google.com/go
- अपने एनवायरमेंट में सही क्रेडेंशियल के साथ Cloud Firestore शुरू करने के लिए, नीचे दिए गए निर्देशों का पालन करें.
PHP
-
Cloud Firestore सर्वर की क्लाइंट लाइब्रेरी (Java, Node.js, Python, Go, PHP, C#, और Ruby) पुष्टि करने के लिए Google ऐप्लिकेशन के डिफ़ॉल्ट क्रेडेंशियल का इस्तेमाल करती हैं.
-
अपने डेवलपमेंट एनवायरमेंट से पुष्टि करने के लिए,
GOOGLE_APPLICATION_CREDENTIALS
एनवायरमेंट वैरिएबल को इस तरह सेट करें कि उपयोगकर्ता, JSON सेवा खाते की कुंजी फ़ाइल पर ले जा सके. आपके पास एपीआई कंसोल के क्रेडेंशियल वाले पेज पर मुख्य फ़ाइल बनाने का विकल्प है.export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json"
- अपने प्रोडक्शन एनवायरमेंट में, आपको यह पुष्टि करने की ज़रूरत नहीं है कि ऐप्लिकेशन को App Engine या Compute Engine पर चलाया जा रहा है. इसके लिए, आपको उसी प्रोजेक्ट का इस्तेमाल करना होगा जिसे Cloud Firestore के लिए इस्तेमाल किया जाता है. अगर ऐसा नहीं है, तो सेवा खाता सेट अप करें.
-
अपने डेवलपमेंट एनवायरमेंट से पुष्टि करने के लिए,
- PHP के लिए gRPC एक्सटेंशन इंस्टॉल और चालू करें. इसके लिए आपको क्लाइंट लाइब्रेरी का इस्तेमाल करना होगा.
-
अपने ऐप्लिकेशन में Cloud Firestore PHP लाइब्रेरी जोड़ें:
composer require google/cloud-firestore
C#
-
Cloud Firestore सर्वर की क्लाइंट लाइब्रेरी (Java, Node.js, Python, Go, PHP, C#, और Ruby) पुष्टि करने के लिए Google ऐप्लिकेशन के डिफ़ॉल्ट क्रेडेंशियल का इस्तेमाल करती हैं.
-
अपने डेवलपमेंट एनवायरमेंट से पुष्टि करने के लिए,
GOOGLE_APPLICATION_CREDENTIALS
एनवायरमेंट वैरिएबल को इस तरह सेट करें कि उपयोगकर्ता, JSON सेवा खाते की कुंजी फ़ाइल पर ले जा सके. आपके पास एपीआई कंसोल के क्रेडेंशियल वाले पेज पर मुख्य फ़ाइल बनाने का विकल्प है.export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json"
- अपने प्रोडक्शन एनवायरमेंट में, आपको यह पुष्टि करने की ज़रूरत नहीं है कि ऐप्लिकेशन को App Engine या Compute Engine पर चलाया जा रहा है. इसके लिए, आपको उसी प्रोजेक्ट का इस्तेमाल करना होगा जिसे Cloud Firestore के लिए इस्तेमाल किया जाता है. अगर ऐसा नहीं है, तो सेवा खाता सेट अप करें.
-
अपने डेवलपमेंट एनवायरमेंट से पुष्टि करने के लिए,
-
अपनी
.csproj
फ़ाइल के ऐप्लिकेशन में, Cloud Firestore C# लाइब्रेरी जोड़ें:<ItemGroup> <PackageReference Include="Google.Cloud.Firestore" Version="1.1.0-beta01" /> </ItemGroup>
-
अपनी
Program.cs
फ़ाइल में यह जोड़ें:using Google.Cloud.Firestore;
Ruby
-
Cloud Firestore सर्वर की क्लाइंट लाइब्रेरी (Java, Node.js, Python, Go, PHP, C#, और Ruby) पुष्टि करने के लिए Google ऐप्लिकेशन के डिफ़ॉल्ट क्रेडेंशियल का इस्तेमाल करती हैं.
-
अपने डेवलपमेंट एनवायरमेंट से पुष्टि करने के लिए,
GOOGLE_APPLICATION_CREDENTIALS
एनवायरमेंट वैरिएबल को इस तरह सेट करें कि उपयोगकर्ता, JSON सेवा खाते की कुंजी फ़ाइल पर ले जा सके. आपके पास एपीआई कंसोल के क्रेडेंशियल वाले पेज पर मुख्य फ़ाइल बनाने का विकल्प है.export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json"
- अपने प्रोडक्शन एनवायरमेंट में, आपको यह पुष्टि करने की ज़रूरत नहीं है कि ऐप्लिकेशन को App Engine या Compute Engine पर चलाया जा रहा है. इसके लिए, आपको उसी प्रोजेक्ट का इस्तेमाल करना होगा जिसे Cloud Firestore के लिए इस्तेमाल किया जाता है. अगर ऐसा नहीं है, तो सेवा खाता सेट अप करें.
-
अपने डेवलपमेंट एनवायरमेंट से पुष्टि करने के लिए,
-
अपने
Gemfile
में, अपने ऐप्लिकेशन में Cloud Firestore Ruby लाइब्रेरी जोड़ें:gem "google-cloud-firestore"
-
अपने
Gemfile
से डिपेंडेंसी इंस्टॉल करें. इसके लिए, इनका इस्तेमाल करें:bundle install
(ज़रूरी नहीं) Firebase लोकल एम्युलेटर सुइट की मदद से प्रोटोटाइप बनाएं और टेस्ट करें
मोबाइल डेवलपर के लिए, आपका ऐप्लिकेशन Cloud Firestore से जुड़े डेटा को कैसे लिखता है और पढ़ता है, इस बारे में बात करने से पहले, हम कुछ टूल के बारे में जानते हैं जिनका इस्तेमाल करके, Cloud Firestore के फ़ंक्शन को प्रोटोटाइप किया जा सकता है और उनकी जांच की जा सकती है: Firebase लोकल एम्युलेटर सुइट. अगर अलग-अलग डेटा मॉडल आज़माए जा रहे हैं, अपने सुरक्षा नियमों को ऑप्टिमाइज़ किया जा रहा है या बैक-एंड से इंटरैक्ट करने का सबसे किफ़ायती तरीका खोजना है, तो लाइव सेवाओं को डिप्लॉय किए बिना, स्थानीय तौर पर काम करना एक अच्छी बात हो सकती है.
Cloud Firestore एम्युलेटर, Local Emulator Suite का हिस्सा होता है. इससे आपका ऐप्लिकेशन, एम्युलेट किए गए डेटाबेस के कॉन्टेंट और कॉन्फ़िगरेशन के साथ इंटरैक्ट कर पाता है. साथ ही, यह एम्युलेट किए गए प्रोजेक्ट के संसाधनों (फ़ंक्शन, दूसरे डेटाबेस, और सुरक्षा के नियम) के साथ भी इंटरैक्ट करने की सुविधा देता है.
Cloud Firestore एम्युलेटर का इस्तेमाल करने के लिए, आपको कुछ ही चरण पूरे करने होंगे:
- एम्युलेटर से कनेक्ट करने के लिए, अपने ऐप्लिकेशन के टेस्ट कॉन्फ़िगरेशन में कोड की एक लाइन जोड़ना.
- आपकी लोकल प्रोजेक्ट डायरेक्ट्री के रूट से,
firebase emulators:start
पर चल रहा है. - हमेशा की तरह, Cloud Firestore प्लैटफ़ॉर्म एसडीके का इस्तेमाल करके, अपने ऐप्लिकेशन के प्रोटोटाइप कोड से कॉल करना.
Cloud Firestore और Cloud Functions के बारे में सिलसिलेवार तरीके से निर्देश उपलब्ध है. आपको लोकल एम्युलेटर सुइट के बारे में जानकारी भी देखनी चाहिए.
Cloud Firestore को शुरू करना
Cloud Firestore का इंस्टेंस शुरू करना:
वेब मॉड्यूलर एपीआई
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
से बदलें.
डिवाइस का कनेक्शन बंद होने पर डेटा सेव रखने के लिए, ऑफ़लाइन डेटा चालू करें का दस्तावेज़ देखें.
वेब नेमस्पेसेड एपीआई
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
से बदलें.
डिवाइस का कनेक्शन बंद होने पर डेटा सेव रखने के लिए, ऑफ़लाइन डेटा चालू करें का दस्तावेज़ देखें.
स्विफ़्ट
import FirebaseCore import FirebaseFirestore
FirebaseApp.configure() let db = Firestore.firestore()
मकसद-सी
@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;
Java
Cloud Firestore SDK टूल को आपके एनवायरमेंट के हिसाब से अलग-अलग तरीकों से शुरू किया जाता है. यहां सबसे आम तरीके दिए गए हैं. पूरी जानकारी के लिए, एडमिन SDK को शुरू करना देखें.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 एडमिन SDK का इस्तेमाल करने के लिए, सेवा खाते का इस्तेमाल करें.
Google Cloud Console में, आईएएम और एडमिन > सेवा खाते पर जाएं. नई निजी कुंजी जनरेट करें और 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 टूल को आपके एनवायरमेंट के हिसाब से अलग-अलग तरीकों से शुरू किया जाता है. यहां सबसे आम तरीके दिए गए हैं. पूरी जानकारी के लिए, एडमिन SDK को शुरू करना देखें.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 एडमिन SDK का इस्तेमाल करने के लिए, सेवा खाते का इस्तेमाल करें.
Google Cloud Console में, आईएएम और एडमिन > सेवा खाते पर जाएं. नई निजी कुंजी जनरेट करें और 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 टूल को आपके एनवायरमेंट के हिसाब से अलग-अलग तरीकों से शुरू किया जाता है. यहां सबसे आम तरीके दिए गए हैं. पूरी जानकारी के लिए, एडमिन SDK को शुरू करना देखें.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 एडमिन SDK का इस्तेमाल करने के लिए, सेवा खाते का इस्तेमाल करें.
Google Cloud Console में, आईएएम और एडमिन > सेवा खाते पर जाएं. नई निजी कुंजी जनरेट करें और 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 टूल को आपके एनवायरमेंट के हिसाब से अलग-अलग तरीकों से शुरू किया जाता है. यहां सबसे आम तरीके दिए गए हैं. पूरी जानकारी के लिए, एडमिन 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 एडमिन SDK टूल का इस्तेमाल करने के लिए, सेवा खाते का इस्तेमाल करें. Google Cloud Console में, आईएएम और एडमिन > सेवा खाते पर जाएं. नई निजी कुंजी जनरेट करें और 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();
शुरू करें
Cloud Firestore SDK टूल को आपके एनवायरमेंट के हिसाब से अलग-अलग तरीकों से शुरू किया जाता है. यहां सबसे आम तरीके दिए गए हैं. पूरी जानकारी के लिए, एडमिन SDK को शुरू करना देखें.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 एडमिन SDK का इस्तेमाल करने के लिए, सेवा खाते का इस्तेमाल करें.
Google Cloud Console में, आईएएम और एडमिन > सेवा खाते पर जाएं. नई निजी कुंजी जनरेट करें और 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 क्लाइंट लाइब्रेरी देखें.
Unity
using Firebase.Firestore; using Firebase.Extensions;
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
C#
C#
Cloud Firestore क्लाइंट बनाने और इंस्टॉल करने के बारे में ज़्यादा जानकारी के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
Ruby
डेटा जोड़ें
Cloud Firestore, दस्तावेज़ों में मौजूद डेटा को कलेक्शन में सेव करता है. Cloud Firestore, सीधे तौर पर दस्तावेज़ में डेटा जोड़ते समय कलेक्शन और दस्तावेज़ बनाता है. आपको कलेक्शन या दस्तावेज़ साफ़ तौर पर बनाने की ज़रूरत नहीं है.
नीचे दिए गए उदाहरण कोड का इस्तेमाल करके, नया कलेक्शन और दस्तावेज़ बनाएं.
वेब मॉड्यूलर एपीआई
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); }
वेब नेमस्पेसेड एपीआई
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)") }
Objective-C
// 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
Python
Python
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 के लिए
शुरू करें
PHP
PHP
Cloud Firestore क्लाइंट बनाने और इंस्टॉल करने के बारे में ज़्यादा जानकारी के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
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#
Ruby
अब users
संग्रह में एक और दस्तावेज़ जोड़ें. ध्यान दें कि इस दस्तावेज़ में एक की-वैल्यू पेयर (बीच का नाम) शामिल है, जो पहले दस्तावेज़ में नहीं दिखता. कलेक्शन के दस्तावेज़ों में जानकारी के अलग-अलग सेट हो सकते हैं.
वेब मॉड्यूलर एपीआई
// 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); }
वेब नेमस्पेसेड एपीआई
// 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)") }
Objective-C
// 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
Python
Python
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 के लिए
शुरू करें
PHP
PHP
Cloud Firestore क्लाइंट बनाने और इंस्टॉल करने के बारे में ज़्यादा जानकारी के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
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#
Ruby
डेटा पढ़ें
Firebase कंसोल में डेटा व्यूअर का इस्तेमाल करके, तुरंत पुष्टि करें कि आपने Cloud Firestore में डेटा जोड़ा है.
पूरे कलेक्शन को वापस पाने के लिए, "get" तरीके का इस्तेमाल भी किया जा सकता है.
वेब मॉड्यूलर एपीआई
import { collection, getDocs } from "firebase/firestore"; const querySnapshot = await getDocs(collection(db, "users")); querySnapshot.forEach((doc) => { console.log(`${doc.id} => ${doc.data()}`); });
वेब नेमस्पेसेड एपीआई
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)") }
Objective-C
[[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
Python
users_ref = db.collection("users") docs = users_ref.stream() for doc in docs: print(f"{doc.id} => {doc.to_dict()}")
Python
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 के लिए
शुरू करें
PHP
PHP
Cloud Firestore क्लाइंट बनाने और इंस्टॉल करने के बारे में ज़्यादा जानकारी के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
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#
Ruby
अपना डेटा सुरक्षित रखें
अगर वेब, Android या Apple प्लैटफ़ॉर्म का SDK टूल इस्तेमाल किया जा रहा है, तो Cloud Firestore में अपना डेटा सुरक्षित रखने के लिए, Firebase से पुष्टि करने और Cloud 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 डेटा ऐक्सेस कर सकें. ऐप्लिकेशन जांच दस्तावेज़ देखें.
अगर किसी सर्वर SDK टूल का इस्तेमाल किया जा रहा है, तो Cloud Firestore में अपने डेटा को सुरक्षित रखने के लिए, Identity and Access Management (IAM) का इस्तेमाल करें.
वीडियो ट्यूटोरियल देखें
Cloud Firestore मोबाइल क्लाइंट लाइब्रेरी का इस्तेमाल शुरू करने के बारे में ज़्यादा जानकारी के लिए, नीचे दिया गया कोई वीडियो ट्यूटोरियल देखें:
वेब
iOS और उसके बाद के वर्शन
Android
Firebase YouTube चैनल पर जाकर और भी वीडियो देखे जा सकते हैं.
अगले चरण
इन विषयों के बारे में बेहतर जानकारी पाएं:
- कोडलैब — Android, iOS या वेब के लिए कोडलैब फ़ॉलो करके, Cloud Firestore को किसी असल ऐप्लिकेशन में इस्तेमाल करना सीखें.
- डेटा मॉडल — Cloud Firestore में डेटा को कैसे स्ट्रक्चर किया जाता है, इस बारे में ज़्यादा जानें. इसमें हैरारकी के हिसाब से डेटा और सब-कलेक्शन भी शामिल हैं.
- डेटा जोड़ें — Cloud Firestore में डेटा बनाने और अपडेट करने के बारे में ज़्यादा जानें.
- डेटा पाएं — डेटा वापस पाने के तरीके के बारे में ज़्यादा जानें.
- आसान और कंपाउंड क्वेरी करना — आसान और कंपाउंड क्वेरी चलाने का तरीका जानें.
- ऑर्डर और क्वेरी को सीमित करने का तरीका जानें कि क्वेरी से मिलने वाले डेटा को ऑर्डर और सीमित कैसे किया जाता है.