Cập nhật mọi thông tin được công bố tại Hội nghị thượng đỉnh Firebase và tìm hiểu cách Firebase có thể giúp bạn đẩy nhanh quá trình phát triển ứng dụng và tự tin chạy ứng dụng của mình. Tìm hiểu thêm
- căn cứ hỏa lực
- Tài liệu
- lò sưởi
- Xây dựng
Có ba cách để lấy dữ liệu được lưu trữ trong Cloud Firestore. Bất kỳ phương pháp nào trong số này đều có thể được sử dụng với tài liệu, bộ sưu tập tài liệu hoặc kết quả của truy vấn
- Gọi một phương thức để lấy dữ liệu một lần
- Đặt trình nghe để nhận các sự kiện thay đổi dữ liệu
- Tải hàng loạt dữ liệu chụp nhanh Firestore từ nguồn bên ngoài thông qua gói dữ liệu. Xem tài liệu gói để biết thêm chi tiết
Khi bạn đặt một trình nghe, Cloud Firestore sẽ gửi cho trình nghe của bạn một ảnh chụp nhanh dữ liệu ban đầu và sau đó là một ảnh chụp nhanh khác mỗi khi tài liệu thay đổi
Ghi chú. Mặc dù các mẫu mã bao gồm nhiều ngôn ngữ, văn bản giải thích các mẫu đề cập đến tên phương thức Web.Dữ liệu mẫu
Để bắt đầu, hãy viết một số dữ liệu về các thành phố để chúng tôi có thể xem xét các cách khác nhau để đọc lại
example_data.jsPhiên bản web 9
Tìm hiểu thêm về SDK mô-đun Web v9 có thể rung chuyển dạng cây và nâng cấp từ phiên bản 8. 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"] }];
Phiên bản web 8
Tìm hiểu thêm về SDK mô-đun Web v9 có thể rung chuyển dạng cây và nâng cấp từ phiên bản 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"] }];test.firestore.js
Ghi chú. Sản phẩm này không khả dụng trên các mục tiêu watchOS và App Clip
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"] ]]ViewController.swift
Ghi chú. Sản phẩm này không khả dụng trên các mục tiêu watchOS và App Clip
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"] }];ViewController.m
Java
CollectionReference cities = db.collection["cities"]; Map 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 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 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 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 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];DocSnippets.java
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]DocSnippets.kt
phi tiêu
final cities = db.collection["cities"]; final data1 = { "name": "San Francisco", "state": "CA", "country": "USA", "capital": false, "population": 860000, "regions": ["west_coast", "norcal"] }; cities.doc["SF"].set[data1]; final data2 = { "name": "Los Angeles", "state": "CA", "country": "USA", "capital": false, "population": 3900000, "regions": ["west_coast", "socal"], }; cities.doc["LA"].set[data2]; final data3 = { "name": "Washington D.C.", "state": null, "country": "USA", "capital": true, "population": 680000, "regions": ["east_coast"] }; cities.doc["DC"].set[data3]; final data4 = { "name": "Tokyo", "state": null, "country": "Japan", "capital": true, "population": 9000000, "regions": ["kanto", "honshu"] }; cities.doc["TOK"].set[data4]; final data5 = { "name": "Beijing", "state": null, "country": "China", "capital": true, "population": 21500000, "regions": ["jingjinji", "hebei"], }; cities.doc["BJ"].set[data5];firestore.dart
CollectionReference cities = db.collection["cities"];
List futures = new ArrayList[];
futures.add[
cities
.document["SF"]
.set[
new City[
"San Francisco",
"CA",
"USA",
false,
860000L,
Arrays.asList["west_coast", "norcal"]]]];
futures.add[
cities
.document["LA"]
.set[
new City[
"Los Angeles",
"CA",
"USA",
false,
3900000L,
Arrays.asList["west_coast", "socal"]]]];
futures.add[
cities
.document["DC"]
.set[
new City[
"Washington D.C.", null, "USA", true, 680000L, Arrays.asList["east_coast"]]]];
futures.add[
cities
.document["TOK"]
.set[
new City[
"Tokyo", null, "Japan", true, 9000000L, Arrays.asList["kanto", "honshu"]]]];
futures.add[
cities
.document["BJ"]
.set[
new City[
"Beijing",
null,
"China",
true,
21500000L,
Arrays.asList["jingjinji", "hebei"]]]];
// [optional] block on operation
ApiFutures.allAsList[futures].get[];RetrieveDataSnippets.java
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[]]snippets.py
con trăn
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"] }];0test.firestore.js
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"] }];1test.firestore.js
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"] }];2test.firestore.js
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"] }];3test.firestore.js
PHP
Để biết thêm về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore