Add a Firestore document using a map

Stay organized with collections Save and categorize content based on your preferences.

Add a Firestore document using a map

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


DocumentReference docRef = db.Collection("cities").Document("LA");
Dictionary<string, object> city = new Dictionary<string, object>
    { "name", "Los Angeles" },
    { "state", "CA" },
    { "country", "USA" }
await docRef.SetAsync(city);


_, err := client.Collection("cities").Doc("LA").Set(ctx, map[string]interface{}{
	"name":    "Los Angeles",
	"state":   "CA",
	"country": "USA",
if err != nil {
	// Handle any errors in an appropriate way, such as returning them.
	log.Printf("An error has occurred: %s", err)


// Create a Map to store the data we want to set
Map<String, Object> docData = new HashMap<>();
docData.put("name", "Los Angeles");
docData.put("state", "CA");
docData.put("country", "USA");
docData.put("regions", Arrays.asList("west_coast", "socal"));
// Add a new document (asynchronously) in collection "cities" with id "LA"
ApiFuture<WriteResult> future = db.collection("cities").document("LA").set(docData);
// ...
// future.get() blocks on response
System.out.println("Update time : " + future.get().getUpdateTime());


const data = {
  name: 'Los Angeles',
  state: 'CA',
  country: 'USA'

// Add a new document in collection "cities" with ID 'LA'
const res = await db.collection('cities').doc('LA').set(data);


$data = [
    'name' => 'Los Angeles',
    'state' => 'CA',
    'country' => 'USA'


data = {
    u'name': u'Los Angeles',
    u'state': u'CA',
    u'country': u'USA'

# Add a new doc in collection 'cities' with ID 'LA'


city_ref = firestore.doc "#{collection_path}/LA"

data = {
  name:    "Los Angeles",
  state:   "CA",
  country: "USA"

city_ref.set data

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.