Firestore 使用入门

Firestore 使用入门

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

Kotlin

// Create the client.
val db = FirestoreOptions.newBuilder()
    .build()
    .service

// Fetch the document reference and data object.
val docRef = db.collection(collectionName).document(documentName)
val data = docRef
    .get() // future
    .get() // snapshot
    .data ?: error("Document $collectionName:$documentName not found") // MutableMap

// Print the retrieved data.
data.forEach { (key, value) -> println("$key: $value") }

Node.js

如需向 Firestore 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

const {Firestore} = require('@google-cloud/firestore');

// Create a new client
const firestore = new Firestore();

async function quickstart() {
  // Obtain a document reference.
  const document = firestore.doc('posts/intro-to-firestore');

  // Enter new data into the document.
  await document.set({
    title: 'Welcome to Firestore',
    body: 'Hello World',
  });
  console.log('Entered new data into the document');

  // Update an existing document.
  await document.update({
    body: 'My first Firestore app',
  });
  console.log('Updated an existing document');

  // Read the document.
  const doc = await document.get();
  console.log('Read the document');

  // Delete the document.
  await document.delete();
  console.log('Deleted the document');
}
quickstart();

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器