Criar uma coleção de documentos do Firestore
Exemplo de código
Go
import (
"context"
"fmt"
"cloud.google.com/go/firestore"
)
// collectionGroupSetup sets up a collection group to query.
func collectionGroupSetup(projectID, cityCollection string) error {
ctx := context.Background()
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
return fmt.Errorf("firestore.NewClient: %v", err)
}
defer client.Close()
landmarks := []struct {
city, name, t string
}{
{"SF", "Golden Gate Bridge", "bridge"},
{"SF", "Legion of Honor", "museum"},
{"LA", "Griffith Park", "park"},
{"LA", "The Getty", "museum"},
{"DC", "Lincoln Memorial", "memorial"},
{"DC", "National Air and Space Museum", "museum"},
{"TOK", "Ueno Park", "park"},
{"TOK", "National Museum of Nature and Science", "museum"},
{"BJ", "Jingshan Park", "park"},
{"BJ", "Beijing Ancient Observatory", "museum"},
}
cities := client.Collection(cityCollection)
for _, l := range landmarks {
if _, err := cities.Doc(l.city).Collection("landmarks").NewDoc().Set(ctx, map[string]string{
"name": l.name,
"type": l.t,
}); err != nil {
return fmt.Errorf("Set: %v", err)
}
}
return nil
}
Java
CollectionReference cities = db.collection("cities");
final List<ApiFuture<WriteResult>> futures =
Arrays.asList(
cities
.document("SF")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "Golden Gate Bridge");
put("type", "bridge");
}
}),
cities
.document("SF")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "Legion of Honor");
put("type", "museum");
}
}),
cities
.document("LA")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "Griffith Park");
put("type", "park");
}
}),
cities
.document("LA")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "The Getty");
put("type", "museum");
}
}),
cities
.document("DC")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "Lincoln Memorial");
put("type", "memorial");
}
}),
cities
.document("DC")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "National Air and Space Museum");
put("type", "museum");
}
}),
cities
.document("TOK")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "Ueno Park");
put("type", "park");
}
}),
cities
.document("TOK")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "National Museum of Nature and Science");
put("type", "museum");
}
}),
cities
.document("BJ")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "Jingshan Park");
put("type", "park");
}
}),
cities
.document("BJ")
.collection("landmarks")
.document()
.set(
new HashMap<String, String>() {
{
put("name", "Beijing Ancient Observatory");
put("type", "museum");
}
}));
final List<WriteResult> landmarks = ApiFutures.allAsList(futures).get();
Node.js
const citiesRef = db.collection('cities');
await citiesRef.doc('SF').collection('landmarks').doc().set({
name: 'Golden Gate Bridge',
type: 'bridge'
});
await citiesRef.doc('SF').collection('landmarks').doc().set({
name: 'Legion of Honor',
type: 'museum'
});
await citiesRef.doc('LA').collection('landmarks').doc().set({
name: 'Griffith Park',
type: 'park'
});
await citiesRef.doc('LA').collection('landmarks').doc().set({
name: 'The Getty',
type: 'museum'
});
await citiesRef.doc('DC').collection('landmarks').doc().set({
name: 'Lincoln Memorial',
type: 'memorial'
});
await citiesRef.doc('DC').collection('landmarks').doc().set({
name: 'National Air and Space Museum',
type: 'museum'
});
await citiesRef.doc('TOK').collection('landmarks').doc().set({
name: 'Ueno Park',
type: 'park'
});
await citiesRef.doc('TOK').collection('landmarks').doc().set({
name: 'National Museum of Nature and Science',
type: 'museum'
});
await citiesRef.doc('BJ').collection('landmarks').doc().set({
name: 'Jingshan Park',
type: 'park'
});
await citiesRef.doc('BJ').collection('landmarks').doc().set({
name: 'Beijing Ancient Observatory',
type: 'museum'
});
Python
cities = db.collection(u'cities')
sf_landmarks = cities.document(u'SF').collection(u'landmarks')
sf_landmarks.document().set({
u'name': u'Golden Gate Bridge',
u'type': u'bridge'
})
sf_landmarks.document().set({
u'name': u'Legion of Honor',
u'type': u'museum'
})
la_landmarks = cities.document(u'LA').collection(u'landmarks')
la_landmarks.document().set({
u'name': u'Griffith Park',
u'type': u'park'
})
la_landmarks.document().set({
u'name': u'The Getty',
u'type': u'museum'
})
dc_landmarks = cities.document(u'DC').collection(u'landmarks')
dc_landmarks.document().set({
u'name': u'Lincoln Memorial',
u'type': u'memorial'
})
dc_landmarks.document().set({
u'name': u'National Air and Space Museum',
u'type': u'museum'
})
tok_landmarks = cities.document(u'TOK').collection(u'landmarks')
tok_landmarks.document().set({
u'name': u'Ueno Park',
u'type': u'park'
})
tok_landmarks.document().set({
u'name': u'National Museum of Nature and Science',
u'type': u'museum'
})
bj_landmarks = cities.document(u'BJ').collection(u'landmarks')
bj_landmarks.document().set({
u'name': u'Jingshan Park',
u'type': u'park'
})
bj_landmarks.document().set({
u'name': u'Beijing Ancient Observatory',
u'type': u'museum'
})
A seguir
Para pesquisar e filtrar amostras de código para outros produtos do Google Cloud, consulte o Navegador de amostra do Google Cloud (em inglês).