Getting started with Firestore
Stay organized with collections
Save and categorize content based on your preferences.
Getting started with Firestore
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis guide provides code samples in Kotlin and Node.js for interacting with Firestore.\u003c/p\u003e\n"],["\u003cp\u003eThe Kotlin code demonstrates how to create a Firestore client, fetch a document reference, retrieve data, and handle potential errors.\u003c/p\u003e\n"],["\u003cp\u003eThe Node.js code illustrates how to create a Firestore client, set, update, read, and delete a document.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Firestore can be set up through Application Default Credentials, as detailed in the linked documentation.\u003c/p\u003e\n"],["\u003cp\u003eYou can explore more code samples for other Google Cloud products using the Google Cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# Getting started with Firestore\n\nCode sample\n-----------\n\n### Kotlin\n\n // Create the client.\n val db = FirestoreOptions.newBuilder()\n .build()\n .service\n\n // Fetch the document reference and data object.\n val docRef = db.collection(collectionName).document(documentName)\n val data = docRef\n .get() // future\n .get() // snapshot\n .data ?: error(\"Document $collectionName:$documentName not found\") // MutableMap\n\n // Print the retrieved data.\n data.forEach { (key, value) -\u003e println(\"$key: $value\") }\n\n### Node.js\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const {Firestore} = require('https://cloud.google.com/nodejs/docs/reference/firestore/latest/overview.html');\n\n // Create a new client\n const firestore = new https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/firestore.html();\n\n async function quickstart() {\n // Obtain a document reference.\n const document = firestore.doc('posts/intro-to-firestore');\n\n // Enter new data into the document.\n await document.set({\n title: 'Welcome to Firestore',\n body: 'Hello World',\n });\n console.log('Entered new data into the document');\n\n // Update an existing document.\n await document.update({\n body: 'My first Firestore app',\n });\n console.log('Updated an existing document');\n\n // Read the document.\n const doc = await document.get();\n console.log('Read the document');\n\n // Delete the document.\n await document.delete();\n console.log('Deleted the document');\n }\n quickstart();\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]