มีสองวิธีในการดึงข้อมูลที่จัดเก็บไว้ใน Cloud Firestore วิธีใดวิธีหนึ่งเหล่านี้สามารถใช้กับเอกสาร คอลเลกชันของเอกสาร หรือผลลัพธ์ของการสืบค้น:
- เรียกวิธีการรับข้อมูล
- ตั้งค่าผู้ฟังเพื่อรับเหตุการณ์การเปลี่ยนแปลงข้อมูล
เมื่อคุณตั้งค่า Listener Cloud Firestore จะส่งสแนปชอตเริ่มต้นของข้อมูลให้ผู้ฟัง จากนั้นจึงส่งสแนปชอตอื่นทุกครั้งที่เอกสารเปลี่ยนแปลง
ตัวอย่างข้อมูล
ในการเริ่มต้น ให้เขียนข้อมูลบางอย่างเกี่ยวกับเมืองเพื่อให้เราสามารถดูวิธีต่างๆ ในการอ่านกลับ:
Web version 9
import { collection, doc, setDoc } from "firebase/firestore"; const citiesRef = collection(db, "cities"); await setDoc(doc(citiesRef, "SF"), { name: "San Francisco", state: "CA", country: "USA", capital: false, population: 860000, regions: ["west_coast", "norcal"] }); await setDoc(doc(citiesRef, "LA"), { name: "Los Angeles", state: "CA", country: "USA", capital: false, population: 3900000, regions: ["west_coast", "socal"] }); await setDoc(doc(citiesRef, "DC"), { name: "Washington, D.C.", state: null, country: "USA", capital: true, population: 680000, regions: ["east_coast"] }); await setDoc(doc(citiesRef, "TOK"), { name: "Tokyo", state: null, country: "Japan", capital: true, population: 9000000, regions: ["kanto", "honshu"] }); await setDoc(doc(citiesRef, "BJ"), { name: "Beijing", state: null, country: "China", capital: true, population: 21500000, regions: ["jingjinji", "hebei"] });
Web version 8
var citiesRef = db.collection("cities"); citiesRef.doc("SF").set({ name: "San Francisco", state: "CA", country: "USA", capital: false, population: 860000, regions: ["west_coast", "norcal"] }); citiesRef.doc("LA").set({ name: "Los Angeles", state: "CA", country: "USA", capital: false, population: 3900000, regions: ["west_coast", "socal"] }); citiesRef.doc("DC").set({ name: "Washington, D.C.", state: null, country: "USA", capital: true, population: 680000, regions: ["east_coast"] }); citiesRef.doc("TOK").set({ name: "Tokyo", state: null, country: "Japan", capital: true, population: 9000000, regions: ["kanto", "honshu"] }); citiesRef.doc("BJ").set({ name: "Beijing", state: null, country: "China", capital: true, population: 21500000, regions: ["jingjinji", "hebei"] });
Swift
let citiesRef = db.collection("cities") citiesRef.document("SF").setData([ "name": "San Francisco", "state": "CA", "country": "USA", "capital": false, "population": 860000, "regions": ["west_coast", "norcal"] ]) citiesRef.document("LA").setData([ "name": "Los Angeles", "state": "CA", "country": "USA", "capital": false, "population": 3900000, "regions": ["west_coast", "socal"] ]) citiesRef.document("DC").setData([ "name": "Washington D.C.", "country": "USA", "capital": true, "population": 680000, "regions": ["east_coast"] ]) citiesRef.document("TOK").setData([ "name": "Tokyo", "country": "Japan", "capital": true, "population": 9000000, "regions": ["kanto", "honshu"] ]) citiesRef.document("BJ").setData([ "name": "Beijing", "country": "China", "capital": true, "population": 21500000, "regions": ["jingjinji", "hebei"] ])
วัตถุประสงค์-C
FIRCollectionReference *citiesRef = [self.db collectionWithPath:@"cities"]; [[citiesRef documentWithPath:@"SF"] setData:@{ @"name": @"San Francisco", @"state": @"CA", @"country": @"USA", @"capital": @(NO), @"population": @860000, @"regions": @[@"west_coast", @"norcal"] }]; [[citiesRef documentWithPath:@"LA"] setData:@{ @"name": @"Los Angeles", @"state": @"CA", @"country": @"USA", @"capital": @(NO), @"population": @3900000, @"regions": @[@"west_coast", @"socal"] }]; [[citiesRef documentWithPath:@"DC"] setData:@{ @"name": @"Washington D.C.", @"country": @"USA", @"capital": @(YES), @"population": @680000, @"regions": @[@"east_coast"] }]; [[citiesRef documentWithPath:@"TOK"] setData:@{ @"name": @"Tokyo", @"country": @"Japan", @"capital": @(YES), @"population": @9000000, @"regions": @[@"kanto", @"honshu"] }]; [[citiesRef documentWithPath:@"BJ"] setData:@{ @"name": @"Beijing", @"country": @"China", @"capital": @(YES), @"population": @21500000, @"regions": @[@"jingjinji", @"hebei"] }];
Java
CollectionReference cities = db.collection("cities"); Map<String, Object> data1 = new HashMap<>(); data1.put("name", "San Francisco"); data1.put("state", "CA"); data1.put("country", "USA"); data1.put("capital", false); data1.put("population", 860000); data1.put("regions", Arrays.asList("west_coast", "norcal")); cities.document("SF").set(data1); Map<String, Object> data2 = new HashMap<>(); data2.put("name", "Los Angeles"); data2.put("state", "CA"); data2.put("country", "USA"); data2.put("capital", false); data2.put("population", 3900000); data2.put("regions", Arrays.asList("west_coast", "socal")); cities.document("LA").set(data2); Map<String, Object> data3 = new HashMap<>(); data3.put("name", "Washington D.C."); data3.put("state", null); data3.put("country", "USA"); data3.put("capital", true); data3.put("population", 680000); data3.put("regions", Arrays.asList("east_coast")); cities.document("DC").set(data3); Map<String, Object> data4 = new HashMap<>(); data4.put("name", "Tokyo"); data4.put("state", null); data4.put("country", "Japan"); data4.put("capital", true); data4.put("population", 9000000); data4.put("regions", Arrays.asList("kanto", "honshu")); cities.document("TOK").set(data4); Map<String, Object> data5 = new HashMap<>(); data5.put("name", "Beijing"); data5.put("state", null); data5.put("country", "China"); data5.put("capital", true); data5.put("population", 21500000); data5.put("regions", Arrays.asList("jingjinji", "hebei")); cities.document("BJ").set(data5);
Kotlin+KTX
val cities = db.collection("cities") val data1 = hashMapOf( "name" to "San Francisco", "state" to "CA", "country" to "USA", "capital" to false, "population" to 860000, "regions" to listOf("west_coast", "norcal") ) cities.document("SF").set(data1) val data2 = hashMapOf( "name" to "Los Angeles", "state" to "CA", "country" to "USA", "capital" to false, "population" to 3900000, "regions" to listOf("west_coast", "socal") ) cities.document("LA").set(data2) val data3 = hashMapOf( "name" to "Washington D.C.", "state" to null, "country" to "USA", "capital" to true, "population" to 680000, "regions" to listOf("east_coast") ) cities.document("DC").set(data3) val data4 = hashMapOf( "name" to "Tokyo", "state" to null, "country" to "Japan", "capital" to true, "population" to 9000000, "regions" to listOf("kanto", "honshu") ) cities.document("TOK").set(data4) val data5 = hashMapOf( "name" to "Beijing", "state" to null, "country" to "China", "capital" to true, "population" to 21500000, "regions" to listOf("jingjinji", "hebei") ) cities.document("BJ").set(data5)
Dart
final cities = db.collection("cities"); final data1 = <String, dynamic>{ "name": "San Francisco", "state": "CA", "country": "USA", "capital": false, "population": 860000, "regions": ["west_coast", "norcal"] }; cities.doc("SF").set(data1); final data2 = <String, dynamic>{ "name": "Los Angeles", "state": "CA", "country": "USA", "capital": false, "population": 3900000, "regions": ["west_coast", "socal"], }; cities.doc("LA").set(data2); final data3 = <String, dynamic>{ "name": "Washington D.C.", "state": null, "country": "USA", "capital": true, "population": 680000, "regions": ["east_coast"] }; cities.doc("DC").set(data3); final data4 = <String, dynamic>{ "name": "Tokyo", "state": null, "country": "Japan", "capital": true, "population": 9000000, "regions": ["kanto", "honshu"] }; cities.doc("TOK").set(data4); final data5 = <String, dynamic>{ "name": "Beijing", "state": null, "country": "China", "capital": true, "population": 21500000, "regions": ["jingjinji", "hebei"], }; cities.doc("BJ").set(data5);
Java
Python
class City(object): def __init__(self, name, state, country, capital=False, population=0, regions=[]): self.name = name self.state = state self.country = country self.capital = capital self.population = population self.regions = regions @staticmethod def from_dict(source): # ... def to_dict(self): # ... def __repr__(self): return( f'City(\ name={self.name}, \ country={self.country}, \ population={self.population}, \ capital={self.capital}, \ regions={self.regions}\ )' )
cities_ref = db.collection(u'cities') cities_ref.document(u'BJ').set( City(u'Beijing', None, u'China', True, 21500000, [u'hebei']).to_dict()) cities_ref.document(u'SF').set( City(u'San Francisco', u'CA', u'USA', False, 860000, [u'west_coast', u'norcal']).to_dict()) cities_ref.document(u'LA').set( City(u'Los Angeles', u'CA', u'USA', False, 3900000, [u'west_coast', u'socal']).to_dict()) cities_ref.document(u'DC').set( City(u'Washington D.C.', None, u'USA', True, 680000, [u'east_coast']).to_dict()) cities_ref.document(u'TOK').set( City(u'Tokyo', None, u'Japan', True, 9000000, [u'kanto', u'honshu']).to_dict())
Python
C++
CollectionReference cities = db->Collection("cities"); cities.Document("SF").Set({ {"name", FieldValue::String("San Francisco")}, {"state", FieldValue::String("CA")}, {"country", FieldValue::String("USA")}, {"capital", FieldValue::Boolean(false)}, {"population", FieldValue::Integer(860000)}, {"regions", FieldValue::Array({FieldValue::String("west_coast"), FieldValue::String("norcal")})}, }); cities.Document("LA").Set({ {"name", FieldValue::String("Los Angeles")}, {"state", FieldValue::String("CA")}, {"country", FieldValue::String("USA")}, {"capital", FieldValue::Boolean(false)}, {"population", FieldValue::Integer(3900000)}, {"regions", FieldValue::Array({FieldValue::String("west_coast"), FieldValue::String("socal")})}, }); cities.Document("DC").Set({ {"name", FieldValue::String("Washington D.C.")}, {"state", FieldValue::Null()}, {"country", FieldValue::String("USA")}, {"capital", FieldValue::Boolean(true)}, {"population", FieldValue::Integer(680000)}, {"regions", FieldValue::Array({FieldValue::String("east_coast")})}, }); cities.Document("TOK").Set({ {"name", FieldValue::String("Tokyo")}, {"state", FieldValue::Null()}, {"country", FieldValue::String("Japan")}, {"capital", FieldValue::Boolean(true)}, {"population", FieldValue::Integer(9000000)}, {"regions", FieldValue::Array({FieldValue::String("kanto"), FieldValue::String("honshu")})}, }); cities.Document("BJ").Set({ {"name", FieldValue::String("Beijing")}, {"state", FieldValue::Null()}, {"country", FieldValue::String("China")}, {"capital", FieldValue::Boolean(true)}, {"population", FieldValue::Integer(21500000)}, {"regions", FieldValue::Array({FieldValue::String("jingjinji"), FieldValue::String("hebei")})}, });
Node.js
ไป
PHP
$citiesRef = $db->collection('samples/php/cities'); $citiesRef->document('SF')->set([ 'name' => 'San Francisco', 'state' => 'CA', 'country' => 'USA', 'capital' => false, 'population' => 860000 ]); $citiesRef->document('LA')->set([ 'name' => 'Los Angeles', 'state' => 'CA', 'country' => 'USA', 'capital' => false, 'population' => 3900000 ]); $citiesRef->document('DC')->set([ 'name' => 'Washington D.C.', 'state' => null, 'country' => 'USA', 'capital' => true, 'population' => 680000 ]); $citiesRef->document('TOK')->set([ 'name' => 'Tokyo', 'state' => null, 'country' => 'Japan', 'capital' => true, 'population' => 9000000 ]); $citiesRef->document('BJ')->set([ 'name' => 'Beijing', 'state' => null, 'country' => 'China', 'capital' => true, 'population' => 21500000 ]); printf('Added example cities data to the cities collection.' . PHP_EOL);
ความสามัคคี
CollectionReference citiesRef = db.Collection("cities"); citiesRef.Document("SF").SetAsync(new Dictionary<string, object>(){ { "Name", "San Francisco" }, { "State", "CA" }, { "Country", "USA" }, { "Capital", false }, { "Population", 860000 } }).ContinueWithOnMainThread(task => citiesRef.Document("LA").SetAsync(new Dictionary<string, object>(){ { "Name", "Los Angeles" }, { "State", "CA" }, { "Country", "USA" }, { "Capital", false }, { "Population", 3900000 } }) ).ContinueWithOnMainThread(task => citiesRef.Document("DC").SetAsync(new Dictionary<string, object>(){ { "Name", "Washington D.C." }, { "State", null }, { "Country", "USA" }, { "Capital", true }, { "Population", 680000 } }) ).ContinueWithOnMainThread(task => citiesRef.Document("TOK").SetAsync(new Dictionary<string, object>(){ { "Name", "Tokyo" }, { "State", null }, { "Country", "Japan" }, { "Capital", true }, { "Population", 9000000 } }) ).ContinueWithOnMainThread(task => citiesRef.Document("BJ").SetAsync(new Dictionary<string, object>(){ { "Name", "Beijing" }, { "State", null }, { "Country", "China" }, { "Capital", true }, { "Population", 21500000 } }) );
ค#
CollectionReference citiesRef = db.Collection("cities"); await citiesRef.Document("SF").SetAsync(new Dictionary<string, object>(){ { "Name", "San Francisco" }, { "State", "CA" }, { "Country", "USA" }, { "Capital", false }, { "Population", 860000 } }); await citiesRef.Document("LA").SetAsync(new Dictionary<string, object>(){ { "Name", "Los Angeles" }, { "State", "CA" }, { "Country", "USA" }, { "Capital", false }, { "Population", 3900000 } }); await citiesRef.Document("DC").SetAsync(new Dictionary<string, object>(){ { "Name", "Washington D.C." }, { "State", null }, { "Country", "USA" }, { "Capital", true }, { "Population", 680000 } }); await citiesRef.Document("TOK").SetAsync(new Dictionary<string, object>(){ { "Name", "Tokyo" }, { "State", null }, { "Country", "Japan" }, { "Capital", true }, { "Population", 9000000 } }); await citiesRef.Document("BJ").SetAsync(new Dictionary<string, object>(){ { "Name", "Beijing" }, { "State", null }, { "Country", "China" }, { "Capital", true }, { "Population", 21500000 } }); Console.WriteLine("Added example cities data to the cities collection.");
ทับทิม
รับเอกสาร
ตัวอย่างต่อไปนี้แสดงวิธีการดึงเนื้อหาของเอกสารเดียวโดยใช้ get()
:
Web version 9
import { doc, getDoc } from "firebase/firestore"; const docRef = doc(db, "cities", "SF"); const docSnap = await getDoc(docRef); if (docSnap.exists()) { console.log("Document data:", docSnap.data()); } else { // doc.data() will be undefined in this case console.log("No such document!"); }
Web version 8
var docRef = db.collection("cities").doc("SF"); docRef.get().then((doc) => { if (doc.exists) { console.log("Document data:", doc.data()); } else { // doc.data() will be undefined in this case console.log("No such document!"); } }).catch((error) => { console.log("Error getting document:", error); });
Swift
let docRef = db.collection("cities").document("SF") docRef.getDocument { (document, error) in if let document = document, document.exists { let dataDescription = document.data().map(String.init(describing:)) ?? "nil" print("Document data: \(dataDescription)") } else { print("Document does not exist") } }
วัตถุประสงค์-C
FIRDocumentReference *docRef = [[self.db collectionWithPath:@"cities"] documentWithPath:@"SF"]; [docRef getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) { if (snapshot.exists) { // Document data may be nil if the document exists but has no keys or values. NSLog(@"Document data: %@", snapshot.data); } else { NSLog(@"Document does not exist"); } }];
Java
DocumentReference docRef = db.collection("cities").document("SF"); docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document.exists()) { Log.d(TAG, "DocumentSnapshot data: " + document.getData()); } else { Log.d(TAG, "No such document"); } } else { Log.d(TAG, "get failed with ", task.getException()); } } });
Kotlin+KTX
val docRef = db.collection("cities").document("SF") docRef.get() .addOnSuccessListener { document -> if (document != null) { Log.d(TAG, "DocumentSnapshot data: ${document.data}") } else { Log.d(TAG, "No such document") } } .addOnFailureListener { exception -> Log.d(TAG, "get failed with ", exception) }
Dart
final docRef = db.collection("cities").doc("SF"); docRef.get().then( (DocumentSnapshot doc) { final data = doc.data() as Map<String, dynamic>; // ... }, onError: (e) => print("Error getting document: $e"), );
Java
Python
Python
C++
DocumentReference doc_ref = db->Collection("cities").Document("SF"); doc_ref.Get().OnCompletion([](const Future<DocumentSnapshot>& future) { if (future.error() == Error::kErrorOk) { const DocumentSnapshot& document = *future.result(); if (document.exists()) { std::cout << "DocumentSnapshot id: " << document.id() << std::endl; } else { std::cout << "no such document" << std::endl; } } else { std::cout << "Get failed with: " << future.error_message() << std::endl; } });
Node.js
ไป
PHP
$docRef = $db->collection('samples/php/cities')->document('SF'); $snapshot = $docRef->snapshot(); if ($snapshot->exists()) { printf('Document data:' . PHP_EOL); print_r($snapshot->data()); } else { printf('Document %s does not exist!' . PHP_EOL, $snapshot->id()); }
ความสามัคคี
DocumentReference docRef = db.Collection("cities").Document("SF"); docRef.GetSnapshotAsync().ContinueWithOnMainThread(task => { DocumentSnapshot snapshot = task.Result; if (snapshot.Exists) { Debug.Log(String.Format("Document data for {0} document:", snapshot.Id)); Dictionary<string, object> city = snapshot.ToDictionary(); foreach (KeyValuePair<string, object> pair in city) { Debug.Log(String.Format("{0}: {1}", pair.Key, pair.Value)); } } else { Debug.Log(String.Format("Document {0} does not exist!", snapshot.Id)); } });
ค#
DocumentReference docRef = db.Collection("cities").Document("SF"); DocumentSnapshot snapshot = await docRef.GetSnapshotAsync(); if (snapshot.Exists) { Console.WriteLine("Document data for {0} document:", snapshot.Id); Dictionary<string, object> city = snapshot.ToDictionary(); foreach (KeyValuePair<string, object> pair in city) { Console.WriteLine("{0}: {1}", pair.Key, pair.Value); } } else { Console.WriteLine("Document {0} does not exist!", snapshot.Id); }
ทับทิม
ตัวเลือกแหล่งที่มา
สำหรับแพลตฟอร์มที่รองรับออฟไลน์ คุณสามารถตั้งค่าตัวเลือก source
เพื่อควบคุมวิธีที่ get
สายใช้แคชออฟไลน์
ตามค่าเริ่มต้น การ get
สายจะพยายามดึงสแนปชอตของเอกสารล่าสุดจากฐานข้อมูลของคุณ บนแพลตฟอร์มที่รองรับออฟไลน์ ไลบรารีไคลเอนต์จะใช้แคชออฟไลน์หากเครือข่ายไม่พร้อมใช้งานหรือหากคำขอหมดเวลา
คุณสามารถระบุตัวเลือก source
ในการเรียก get()
เพื่อเปลี่ยนการทำงานเริ่มต้น คุณสามารถดึงข้อมูลจากฐานข้อมูลเท่านั้นและละเว้นแคชออฟไลน์ หรือคุณสามารถดึงจากแคชออฟไลน์เท่านั้น ตัวอย่างเช่น:
Web version 9
import { doc, getDocFromCache } from "firebase/firestore"; const docRef = doc(db, "cities", "SF"); // Get a document, forcing the SDK to fetch from the offline cache. try { const doc = await getDocFromCache(docRef); // Document was found in the cache. If no cached document exists, // an error will be returned to the 'catch' block below. console.log("Cached document data:", doc.data()); } catch (e) { console.log("Error getting cached document:", e); }
Web version 8
var docRef = db.collection("cities").doc("SF"); // Valid options for source are 'server', 'cache', or // 'default'. See https://firebase.google.com/docs/reference/js/firebase.firestore.GetOptions // for more information. var getOptions = { source: 'cache' }; // Get a document, forcing the SDK to fetch from the offline cache. docRef.get(getOptions).then((doc) => { // Document was found in the cache. If no cached document exists, // an error will be returned to the 'catch' block below. console.log("Cached document data:", doc.data()); }).catch((error) => { console.log("Error getting cached document:", error); });
Swift
let docRef = db.collection("cities").document("SF") // Force the SDK to fetch the document from the cache. Could also specify // FirestoreSource.server or FirestoreSource.default. docRef.getDocument(source: .cache) { (document, error) in if let document = document { let dataDescription = document.data().map(String.init(describing:)) ?? "nil" print("Cached document data: \(dataDescription)") } else { print("Document does not exist in cache") } }
วัตถุประสงค์-C
FIRDocumentReference *docRef = [[self.db collectionWithPath:@"cities"] documentWithPath:@"SF"]; // Force the SDK to fetch the document from the cache. Could also specify // FIRFirestoreSourceServer or FIRFirestoreSourceDefault. [docRef getDocumentWithSource:FIRFirestoreSourceCache completion:^(FIRDocumentSnapshot *snapshot, NSError *error) { if (snapshot != NULL) { // The document data was found in the cache. NSLog(@"Cached document data: %@", snapshot.data); } else { // The document data was not found in the cache. NSLog(@"Document does not exist in cache: %@", error); } }];
Java
DocumentReference docRef = db.collection("cities").document("SF"); // Source can be CACHE, SERVER, or DEFAULT. Source source = Source.CACHE; // Get the document, forcing the SDK to use the offline cache docRef.get(source).addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { // Document found in the offline cache DocumentSnapshot document = task.getResult(); Log.d(TAG, "Cached document data: " + document.getData()); } else { Log.d(TAG, "Cached get failed: ", task.getException()); } } });
Kotlin+KTX
val docRef = db.collection("cities").document("SF") // Source can be CACHE, SERVER, or DEFAULT. val source = Source.CACHE // Get the document, forcing the SDK to use the offline cache docRef.get(source).addOnCompleteListener { task -> if (task.isSuccessful) { // Document found in the offline cache val document = task.result Log.d(TAG, "Cached document data: ${document?.data}") } else { Log.d(TAG, "Cached get failed: ", task.exception) } }
Dart
final docRef = db.collection("cities").doc("SF"); // Source can be CACHE, SERVER, or DEFAULT. const source = Source.cache; docRef.get(const GetOptions(source: source)).then( (res) => print("Successfully completed"), onError: (e) => print("Error completing: $e"), );
Java
ไม่รองรับใน Java SDK
Python
ไม่รองรับใน Python SDK
C++
DocumentReference doc_ref = db->Collection("cities").Document("SF"); Source source = Source::kCache; doc_ref.Get(source).OnCompletion([](const Future<DocumentSnapshot>& future) { if (future.error() == Error::kErrorOk) { const DocumentSnapshot& document = *future.result(); if (document.exists()) { std::cout << "Cached document id: " << document.id() << std::endl; } else { } } else { std::cout << "Cached get failed: " << future.error_message() << std::endl; } });
Node.js
ไม่รองรับใน Node.js SDK
ไป
ไม่รองรับใน Go SDK
PHP
ไม่รองรับใน PHP SDK
ความสามัคคี
ไม่รองรับใน Unity SDK
ค#
ไม่รองรับใน C# SDK
ทับทิม
ไม่รองรับ Ruby SDK
วัตถุที่กำหนดเอง
ตัวอย่างก่อนหน้านี้ดึงเนื้อหาของเอกสารเป็นแผนที่ แต่ในบางภาษามักจะสะดวกกว่าที่จะใช้ประเภทวัตถุที่กำหนดเอง ใน Add Data คุณกำหนดคลาส City
ที่คุณใช้กำหนดแต่ละเมือง คุณสามารถเปลี่ยนเอกสารของคุณกลับเป็นวัตถุ City
:
ในการใช้อ็อบเจ็กต์แบบกำหนดเอง คุณต้องกำหนดฟังก์ชัน FirestoreDataConverter สำหรับคลาสของคุณ ตัวอย่างเช่น:
Web version 9
class City { constructor (name, state, country ) { this.name = name; this.state = state; this.country = country; } toString() { return this.name + ', ' + this.state + ', ' + this.country; } } // Firestore data converter const cityConverter = { toFirestore: (city) => { return { name: city.name, state: city.state, country: city.country }; }, fromFirestore: (snapshot, options) => { const data = snapshot.data(options); return new City(data.name, data.state, data.country); } };
ในการใช้อ็อบเจ็กต์แบบกำหนดเอง คุณต้องกำหนดฟังก์ชัน FirestoreDataConverter สำหรับคลาสของคุณ ตัวอย่างเช่น:
Web version 8
class City { constructor (name, state, country ) { this.name = name; this.state = state; this.country = country; } toString() { return this.name + ', ' + this.state + ', ' + this.country; } } // Firestore data converter var cityConverter = { toFirestore: function(city) { return { name: city.name, state: city.state, country: city.country }; }, fromFirestore: function(snapshot, options){ const data = snapshot.data(options); return new City(data.name, data.state, data.country); } };
เรียกโปรแกรมแปลงข้อมูลของคุณด้วยการดำเนินการอ่าน หลังจากการแปลง คุณสามารถเข้าถึงเมธอดของออบเจ็กต์ที่กำหนดเองได้:
Web version 9
import { doc, getDoc} from "firebase/firestore"; const ref = doc(db, "cities", "LA").withConverter(cityConverter); const docSnap = await getDoc(ref); if (docSnap.exists()) { // Convert to City object const city = docSnap.data(); // Use a City instance method console.log(city.toString()); } else { console.log("No such document!"); }
เรียกโปรแกรมแปลงข้อมูลของคุณด้วยการดำเนินการอ่าน หลังจากการแปลง คุณสามารถเข้าถึงเมธอดของออบเจ็กต์ที่กำหนดเองได้:
Web version 8
db.collection("cities").doc("LA") .withConverter(cityConverter) .get().then((doc) => { if (doc.exists){ // Convert to City object var city = doc.data(); // Use a City instance method console.log(city.toString()); } else { console.log("No such document!"); }}).catch((error) => { console.log("Error getting document:", error); });
Swift
เพื่อรองรับการจัดประเภทอัตโนมัติใน Swift ประเภทของคุณจะต้องสอดคล้องกับ โปรโตคอล Codable และคุณต้องรวม pod 'FirebaseFirestoreSwift'
เป็นการพึ่งพาโปรเจ็กต์ของคุณ
let docRef = db.collection("cities").document("BJ") docRef.getDocument(as: City.self) { result in // The Result type encapsulates deserialization errors or // successful deserialization, and can be handled as follows: // // Result // /\ // Error City switch result { case .success(let city): // A `City` value was successfully initialized from the DocumentSnapshot. print("City: \(city)") case .failure(let error): // A `City` value could not be initialized from the DocumentSnapshot. print("Error decoding city: \(error)") } }
วัตถุประสงค์-C
ใน Objective-C คุณต้องดำเนินการด้วยตนเอง
FIRDocumentReference *docRef = [[self.db collectionWithPath:@"cities"] documentWithPath:@"BJ"]; [docRef getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) { FSTCity *city = [[FSTCity alloc] initWithDictionary:snapshot.data]; if (city != nil) { NSLog(@"City: %@", city); } else { NSLog(@"Document does not exist"); } }];
Java
สำคัญ: แต่ละคลาสที่กำหนดเองต้องมีตัวสร้างสาธารณะที่ไม่มีอาร์กิวเมนต์ นอกจากนี้ คลาสต้องมีผู้รับสาธารณะสำหรับแต่ละพร็อพเพอร์ตี้
DocumentReference docRef = db.collection("cities").document("BJ"); docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() { @Override public void onSuccess(DocumentSnapshot documentSnapshot) { City city = documentSnapshot.toObject(City.class); } });
Kotlin+KTX
val docRef = db.collection("cities").document("BJ") docRef.get().addOnSuccessListener { documentSnapshot -> val city = documentSnapshot.toObject<City>() }
Dart
ในการใช้ออบเจ็กต์ที่กำหนดเอง คุณต้องกำหนดฟังก์ชันการแปลงข้อมูล Firestore สำหรับชั้นเรียนของคุณ ตัวอย่างเช่น:
class City { final String? name; final String? state; final String? country; final bool? capital; final int? population; final List<String>? regions; City({ this.name, this.state, this.country, this.capital, this.population, this.regions, }); factory City.fromFirestore( DocumentSnapshot<Map<String, dynamic>> snapshot, SnapshotOptions? options, ) { final data = snapshot.data(); return City( name: data?['name'], state: data?['state'], country: data?['country'], capital: data?['capital'], population: data?['population'], regions: data?['regions'] is Iterable ? List.from(data?['regions']) : null, ); } Map<String, dynamic> toFirestore() { return { if (name != null) "name": name, if (state != null) "state": state, if (country != null) "country": country, if (capital != null) "capital": capital, if (population != null) "population": population, if (regions != null) "regions": regions, }; } }
จากนั้น สร้างเอกสารอ้างอิงด้วยฟังก์ชันการแปลงข้อมูลของคุณ การดำเนินการอ่านใดๆ ที่คุณดำเนินการโดยใช้การอ้างอิงนี้จะส่งคืนอินสแตนซ์ของคลาสที่กำหนดเองของคุณ:
final ref = db.collection("cities").doc("LA").withConverter( fromFirestore: City.fromFirestore, toFirestore: (City city, _) => city.toFirestore(), ); final docSnap = await ref.get(); final city = docSnap.data(); // Convert to City object if (city != null) { print(city); } else { print("No such document."); }
Java
คลาสที่กำหนดเองแต่ละคลาสต้องมีตัวสร้างสาธารณะที่ไม่มีข้อโต้แย้ง นอกจากนี้ คลาสต้องมีผู้รับสาธารณะสำหรับแต่ละพร็อพเพอร์ตี้
Python
Python
C++
// This is not yet supported.
Node.js
Node.js ใช้วัตถุ JavaScript
ไป
PHP
ไม่สามารถใช้ได้กับ PHP
ความสามัคคี
DocumentReference docRef = db.Collection("cities").Document("BJ"); docRef.GetSnapshotAsync().ContinueWith((task) => { var snapshot = task.Result; if (snapshot.Exists) { Debug.Log(String.Format("Document data for {0} document:", snapshot.Id)); City city = snapshot.ConvertTo<City>(); Debug.Log(String.Format("Name: {0}", city.Name)); Debug.Log(String.Format("State: {0}", city.State)); Debug.Log(String.Format("Country: {0}", city.Country)); Debug.Log(String.Format("Capital: {0}", city.Capital)); Debug.Log(String.Format("Population: {0}", city.Population)); } else { Debug.Log(String.Format("Document {0} does not exist!", snapshot.Id)); } });
ค#
DocumentReference docRef = db.Collection("cities").Document("BJ"); DocumentSnapshot snapshot = await docRef.GetSnapshotAsync(); if (snapshot.Exists) { Console.WriteLine("Document data for {0} document:", snapshot.Id); City city = snapshot.ConvertTo<City>(); Console.WriteLine("Name: {0}", city.Name); Console.WriteLine("State: {0}", city.State); Console.WriteLine("Country: {0}", city.Country); Console.WriteLine("Capital: {0}", city.Capital); Console.WriteLine("Population: {0}", city.Population); } else { Console.WriteLine("Document {0} does not exist!", snapshot.Id); }
ทับทิม
ไม่สามารถใช้ได้กับ Ruby
รับเอกสารหลายชุดจากคอลเล็กชัน
คุณยังสามารถเรียกเอกสารหลายฉบับด้วยคำขอเดียวโดยสอบถามเอกสารในคอลเลกชั่น ตัวอย่างเช่น คุณสามารถใช้ where()
เพื่อค้นหาเอกสารทั้งหมดที่ตรงตามเงื่อนไขที่กำหนด จากนั้นใช้ get()
เพื่อดึงผลลัพธ์:
Web version 9
import { collection, query, where, getDocs } from "firebase/firestore"; const q = query(collection(db, "cities"), where("capital", "==", true)); const querySnapshot = await getDocs(q); querySnapshot.forEach((doc) => { // doc.data() is never undefined for query doc snapshots console.log(doc.id, " => ", doc.data()); });
Web version 8
db.collection("cities").where("capital", "==", true) .get() .then((querySnapshot) => { querySnapshot.forEach((doc) => { // doc.data() is never undefined for query doc snapshots console.log(doc.id, " => ", doc.data()); }); }) .catch((error) => { console.log("Error getting documents: ", error); });
Swift
db.collection("cities").whereField("capital", isEqualTo: true) .getDocuments() { (querySnapshot, err) in if let err = err { print("Error getting documents: \(err)") } else { for document in querySnapshot!.documents { print("\(document.documentID) => \(document.data())") } } }
วัตถุประสงค์-C
[[[self.db collectionWithPath:@"cities"] queryWhereField:@"capital" isEqualTo:@(YES)] getDocumentsWithCompletion:^(FIRQuerySnapshot *snapshot, NSError *error) { if (error != nil) { NSLog(@"Error getting documents: %@", error); } else { for (FIRDocumentSnapshot *document in snapshot.documents) { NSLog(@"%@ => %@", document.documentID, document.data); } } }];
Java
db.collection("cities") .whereEqualTo("capital", true) .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.d(TAG, "Error getting documents: ", task.getException()); } } });
Kotlin+KTX
db.collection("cities") .whereEqualTo("capital", true) .get() .addOnSuccessListener { documents -> for (document in documents) { Log.d(TAG, "${document.id} => ${document.data}") } } .addOnFailureListener { exception -> Log.w(TAG, "Error getting documents: ", exception) }
Dart
db.collection("cities").where("capital", isEqualTo: true).get().then( (res) => print("Successfully completed"), onError: (e) => print("Error completing: $e"), );
Java
Python
Python
C++
db->Collection("cities") .WhereEqualTo("capital", FieldValue::Boolean(true)) .Get() .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
$citiesRef = $db->collection('samples/php/cities'); $query = $citiesRef->where('capital', '=', true); $documents = $query->documents(); foreach ($documents as $document) { if ($document->exists()) { printf('Document data for document %s:' . PHP_EOL, $document->id()); print_r($document->data()); printf(PHP_EOL); } else { printf('Document %s does not exist!' . PHP_EOL, $document->id()); } }
ความสามัคคี
Query capitalQuery = db.Collection("cities").WhereEqualTo("Capital", true); capitalQuery.GetSnapshotAsync().ContinueWithOnMainThread(task => { QuerySnapshot capitalQuerySnapshot = task.Result; foreach (DocumentSnapshot documentSnapshot in capitalQuerySnapshot.Documents) { Debug.Log(String.Format("Document data for {0} document:", documentSnapshot.Id)); Dictionary<string, object> city = documentSnapshot.ToDictionary(); foreach (KeyValuePair<string, object> pair in city) { Debug.Log(String.Format("{0}: {1}", pair.Key, pair.Value)); } // Newline to separate entries Debug.Log(""); }; });
ค#
Query capitalQuery = db.Collection("cities").WhereEqualTo("Capital", true); QuerySnapshot capitalQuerySnapshot = await capitalQuery.GetSnapshotAsync(); foreach (DocumentSnapshot documentSnapshot in capitalQuerySnapshot.Documents) { Console.WriteLine("Document data for {0} document:", documentSnapshot.Id); Dictionary<string, object> city = documentSnapshot.ToDictionary(); foreach (KeyValuePair<string, object> pair in city) { Console.WriteLine("{0}: {1}", pair.Key, pair.Value); } Console.WriteLine(""); }
ทับทิม
โดยค่าเริ่มต้น Cloud Firestore จะดึงเอกสารทั้งหมดที่ตอบสนองการสืบค้นโดยเรียงลำดับจากน้อยไปมากตามรหัสเอกสาร แต่คุณสามารถ สั่งซื้อและจำกัดข้อมูลที่ส่งคืน ได้
รับเอกสารทั้งหมดในคอลเลกชัน
นอกจากนี้ คุณสามารถดึงเอกสาร ทั้งหมด ในคอลเล็กชันโดยละเว้นตัวกรอง where()
ทั้งหมด:
Web version 9
import { collection, getDocs } from "firebase/firestore"; const querySnapshot = await getDocs(collection(db, "cities")); querySnapshot.forEach((doc) => { // doc.data() is never undefined for query doc snapshots console.log(doc.id, " => ", doc.data()); });
Web version 8
db.collection("cities").get().then((querySnapshot) => { querySnapshot.forEach((doc) => { // doc.data() is never undefined for query doc snapshots console.log(doc.id, " => ", doc.data()); }); });
Swift
db.collection("cities").getDocuments() { (querySnapshot, err) in if let err = err { print("Error getting documents: \(err)") } else { for document in querySnapshot!.documents { print("\(document.documentID) => \(document.data())") } } }
วัตถุประสงค์-C
[[self.db collectionWithPath:@"cities"] getDocumentsWithCompletion:^(FIRQuerySnapshot *snapshot, NSError *error) { if (error != nil) { NSLog(@"Error getting documents: %@", error); } else { for (FIRDocumentSnapshot *document in snapshot.documents) { NSLog(@"%@ => %@", document.documentID, document.data); } } }];
Java
db.collection("cities") .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.d(TAG, "Error getting documents: ", task.getException()); } } });
Kotlin+KTX
db.collection("cities") .get() .addOnSuccessListener { result -> for (document in result) { Log.d(TAG, "${document.id} => ${document.data}") } } .addOnFailureListener { exception -> Log.d(TAG, "Error getting documents: ", exception) }
Dart
db.collection("cities").get().then( (res) => print("Successfully completed"), onError: (e) => print("Error completing: $e"), );
Java
Python
Python
C++
db->Collection("cities").Get().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
$citiesRef = $db->collection('samples/php/cities'); $documents = $citiesRef->documents(); foreach ($documents as $document) { if ($document->exists()) { printf('Document data for document %s:' . PHP_EOL, $document->id()); print_r($document->data()); printf(PHP_EOL); } else { printf('Document %s does not exist!' . PHP_EOL, $document->id()); } }
ความสามัคคี
Query allCitiesQuery = db.Collection("cities"); allCitiesQuery.GetSnapshotAsync().ContinueWithOnMainThread(task => { QuerySnapshot allCitiesQuerySnapshot = task.Result; foreach (DocumentSnapshot documentSnapshot in allCitiesQuerySnapshot.Documents) { Debug.Log(String.Format("Document data for {0} document:", documentSnapshot.Id)); Dictionary<string, object> city = documentSnapshot.ToDictionary(); foreach (KeyValuePair<string, object> pair in city) { Debug.Log(String.Format("{0}: {1}", pair.Key, pair.Value)); } // Newline to separate entries Debug.Log(""); } });
ค#
Query allCitiesQuery = db.Collection("cities"); QuerySnapshot allCitiesQuerySnapshot = await allCitiesQuery.GetSnapshotAsync(); foreach (DocumentSnapshot documentSnapshot in allCitiesQuerySnapshot.Documents) { Console.WriteLine("Document data for {0} document:", documentSnapshot.Id); Dictionary<string, object> city = documentSnapshot.ToDictionary(); foreach (KeyValuePair<string, object> pair in city) { Console.WriteLine("{0}: {1}", pair.Key, pair.Value); } Console.WriteLine(""); }
ทับทิม
รับเอกสารหลายชุดจากกลุ่มคอลเลกชัน
กลุ่มคอลเลกชันประกอบด้วยคอลเลกชันทั้งหมดที่มี ID เดียวกัน ตัวอย่างเช่น หากเอกสารแต่ละฉบับในคอลเล็กชัน cities
ของคุณมีคอลเล็กชันย่อยที่เรียกว่า landmarks
landmarks
จะอยู่ในกลุ่มคอลเล็กชันเดียวกัน ตามค่าเริ่มต้น คิวรีจะดึงผลลัพธ์จากคอลเล็กชันเดียวในฐานข้อมูลของคุณ ใช้แบบสอบถามกลุ่มคอลเลกชันเพื่อดึงผลลัพธ์จากกลุ่มคอลเลกชัน แทนที่จะเป็นคอลเลกชันเดียว
แสดงรายการคอลเลกชันย่อยของเอกสาร
listCollections()
ของไลบรารีไคลเอ็นต์เซิร์ฟเวอร์ Cloud Firestore แสดงรายการคอลเล็กชันย่อยทั้งหมดของการอ้างอิงเอกสาร
ไม่สามารถดึงรายการคอลเลกชันด้วยไลบรารีไคลเอนต์มือถือ/เว็บ คุณควรค้นหาชื่อคอลเลกชันเป็นส่วนหนึ่งของงานการดูแลระบบในสภาพแวดล้อมเซิร์ฟเวอร์ที่เชื่อถือได้เท่านั้น หากคุณพบว่าคุณต้องการความสามารถนี้ในไลบรารีไคลเอนต์มือถือ/เว็บ ให้พิจารณาปรับโครงสร้างข้อมูลของคุณใหม่เพื่อให้สามารถคาดเดาชื่อคอลเลกชันย่อยได้
เว็บ
ไม่พร้อมใช้งานในไลบรารีเว็บไคลเอ็นต์
Swift
ไม่พร้อมใช้งานในไลบรารีไคลเอนต์ Swift
วัตถุประสงค์-C
ไม่พร้อมใช้งานในไลบรารีไคลเอ็นต์ Objective-C
Java
ไม่พร้อมใช้งานในไลบรารีไคลเอนต์ Android
Kotlin+KTX
ไม่พร้อมใช้งานในไลบรารีไคลเอนต์ Android
Dart
ไม่พร้อมใช้งานในไลบรารีไคลเอ็นต์ Flutter
Java
Python
Python
C++
ไม่พร้อมใช้งานในไลบรารีไคลเอนต์ C++
Node.js
ไป
PHP
$cityRef = $db->collection('samples/php/cities')->document('SF'); $collections = $cityRef->collections(); foreach ($collections as $collection) { printf('Found subcollection with id: %s' . PHP_EOL, $collection->id()); }
ความสามัคคี
// This is not yet supported in the Unity SDK.
ค#
DocumentReference cityRef = db.Collection("cities").Document("SF"); IAsyncEnumerable<CollectionReference> subcollections = cityRef.ListCollectionsAsync(); IAsyncEnumerator<CollectionReference> subcollectionsEnumerator = subcollections.GetAsyncEnumerator(default); while (await subcollectionsEnumerator.MoveNextAsync()) { CollectionReference subcollectionRef = subcollectionsEnumerator.Current; Console.WriteLine("Found subcollection with ID: {0}", subcollectionRef.Id); }
ทับทิม
เรียนรู้เพิ่มเติมเกี่ยวกับ การค้นหาประเภทต่างๆ