當前位置: 首頁>>代碼示例>>Java>>正文


Java DocumentReference類代碼示例

本文整理匯總了Java中com.google.cloud.firestore.DocumentReference的典型用法代碼示例。如果您正苦於以下問題:Java DocumentReference類的具體用法?Java DocumentReference怎麽用?Java DocumentReference使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


DocumentReference類屬於com.google.cloud.firestore包,在下文中一共展示了DocumentReference類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testFirestoreAccess

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
@Test
public void testFirestoreAccess() throws Exception {
  Firestore firestore = FirestoreClient.getFirestore(IntegrationTestUtils.ensureDefaultApp());
  DocumentReference reference = firestore.collection("cities").document("Mountain View");
  ImmutableMap<String, Object> expected = ImmutableMap.<String, Object>of(
      "name", "Mountain View",
      "country", "USA",
      "population", 77846L,
      "capital", false
  );
  WriteResult result = reference.set(expected).get();
  assertNotNull(result);

  Map<String, Object> data = reference.get().get().getData();
  assertEquals(expected.size(), data.size());
  for (Map.Entry<String, Object> entry : expected.entrySet()) {
    assertEquals(entry.getValue(), data.get(entry.getKey()));
  }

  reference.delete().get();
  assertFalse(reference.get().get().exists());
}
 
開發者ID:firebase,項目名稱:firebase-admin-java,代碼行數:23,代碼來源:FirestoreClientIT.java

示例2: addDocumentDataAfterAutoGeneratingId

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/**
 * Add data to a document after generating the document id.
 *
 * @return auto generated id
 */
String addDocumentDataAfterAutoGeneratingId() throws Exception {
  City data = new City();

  // [START fs_add_doc_data_after_auto_id]
  // Add document data after generating an id.
  DocumentReference addedDocRef = db.collection("cities").document();
  System.out.println("Added document with ID: " + addedDocRef.getId());

  // later...
  ApiFuture<WriteResult> writeResult = addedDocRef.set(data);
  // [END fs_add_doc_data_after_auto_id]

  // writeResult.get() blocks on operation
  System.out.println("Update time : " + writeResult.get().getUpdateTime());
  return addedDocRef.getId();
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:22,代碼來源:ManageDataSnippets.java

示例3: updateUsingMap

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/** Partially update fields of a document using a map (field => value). */
void updateUsingMap() throws Exception {
  db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
  // [START fs_update_doc_map]
  // update multiple fields using a map
  DocumentReference docRef = db.collection("cities").document("DC");

  Map<String, Object> updates = new HashMap<>();
  updates.put("name", "Washington D.C.");
  updates.put("country", "USA");
  updates.put("capital", true);

  //asynchronously update doc
  ApiFuture<WriteResult> writeResult = docRef.update(updates);
  // ...
  System.out.println("Update time : " + writeResult.get().getUpdateTime());
  // [END fs_update_doc_map]
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:19,代碼來源:ManageDataSnippets.java

示例4: getADocumentRef

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/**
 * Return a reference to a document.
 *
 * @return document reference
 */
public DocumentReference getADocumentRef() {
  // [START fs_document_ref]
  // Reference to a document with id "alovelace" in the collection "employees"
  DocumentReference document = db.collection("users").document("alovelace");
  // [END fs_document_ref]
  return document;
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:13,代碼來源:References.java

示例5: getADocumentRefUsingPath

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/**
 * Return a reference to a document using path.
 *
 * @return document reference
 */
public DocumentReference getADocumentRefUsingPath() {
  // [START fs_document_path_ref]
  // Reference to a document with id "alovelace" in the collection "employees"
  DocumentReference document = db.document("users/alovelace");
  // [END fs_document_path_ref]
  return document;
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:13,代碼來源:References.java

示例6: getASubCollectionDocumentRef

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/**
 * Return a reference to a document in a sub-collection.
 *
 * @return document reference in a subcollection
 */
public DocumentReference getASubCollectionDocumentRef() {
  // [START fs_subcollection_ref]
  // Reference to a document in subcollection "messages"
  DocumentReference document =
      db.collection("rooms").document("roomA").collection("messages").document("message1");
  // [END fs_subcollection_ref]
  return document;
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:14,代碼來源:References.java

示例7: addDocumentDataWithAutoGeneratedId

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/**
 * Add a document without explicitly providing the document id. The document id gets automatically
 * generated.
 *
 * @return auto generated id
 */
String addDocumentDataWithAutoGeneratedId() throws Exception {
  // [START fs_add_doc_data_with_auto_id]
  // Add document data with auto-generated id.
  Map<String, Object> data = new HashMap<>();
  data.put("name", "Tokyo");
  data.put("country", "Japan");
  ApiFuture<DocumentReference> addedDocRef = db.collection("cities").add(data);
  System.out.println("Added document with ID: " + addedDocRef.get().getId());
  // [END fs_add_doc_data_with_auto_id]

  return addedDocRef.get().getId();
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:19,代碼來源:ManageDataSnippets.java

示例8: updateSimpleDocument

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/** Partially update a document using the .update(field1, value1..) method. */
void updateSimpleDocument() throws Exception {
  db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
  // [START fs_update_doc]
  // Update an existing document
  DocumentReference docRef = db.collection("cities").document("DC");

  // (async) Update one field
  ApiFuture<WriteResult> future = docRef.update("capital", true);

  // ...
  WriteResult result = future.get();
  System.out.println("Write result: " + result);
  // [END fs_update_doc]
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:16,代碼來源:ManageDataSnippets.java

示例9: updateNestedFields

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/** Partial update nested fields of a document. */
void updateNestedFields() throws Exception {
  //CHECKSTYLE OFF: VariableDeclarationUsageDistance
  // [START fs_update_nested_fields]
  // Create an initial document to update
  DocumentReference frankDocRef = db.collection("users").document("frank");
  Map<String, Object> initialData = new HashMap<>();
  initialData.put("name", "Frank");
  initialData.put("age", 12);

  Map<String, Object> favorites = new HashMap<>();
  favorites.put("food", "Pizza");
  favorites.put("color", "Blue");
  favorites.put("subject", "Recess");
  initialData.put("favorites", favorites);

  ApiFuture<WriteResult> initialResult = frankDocRef.set(initialData);
  // Confirm that data has been successfully saved by blocking on the operation
  initialResult.get();

  // Update age and favorite color
  Map<String, Object> updates = new HashMap<>();
  updates.put("age", 13);
  updates.put("favorites.color", "Red");

  // Async update document
  ApiFuture<WriteResult> writeResult = frankDocRef.update(updates);
  // ...
  System.out.println("Update time : " + writeResult.get().getUpdateTime());
  // [END fs_update_nested_fields]
  //CHECKSTYLE ON: VariableDeclarationUsageDistance
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:33,代碼來源:ManageDataSnippets.java

示例10: updateServerTimestamp

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/** Update document with server timestamp. */
void updateServerTimestamp() throws Exception {
  db.collection("objects").document("some-id").set(new HashMap<String, Object>()).get();

  // [START fs_update_server_timestamp]
  DocumentReference docRef = db.collection("objects").document("some-id");
  // Update the timestamp field with the value from the server
  ApiFuture<WriteResult> writeResult = docRef.update("timestamp", FieldValue.serverTimestamp());
  System.out.println("Update time : " + writeResult.get());
  // [END fs_update_server_timestamp]
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:12,代碼來源:ManageDataSnippets.java

示例11: deleteFields

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/** Delete specific fields when updating a document. */
void deleteFields() throws Exception {
  City city = new City("Beijing");
  city.setCapital(true);
  db.collection("cities").document("BJ").set(city).get();

  // [START fs_delete_fields]
  DocumentReference docRef = db.collection("cities").document("BJ");
  Map<String, Object> updates = new HashMap<>();
  updates.put("capital", FieldValue.delete());
  // Update and delete the "capital" field in the document
  ApiFuture<WriteResult> writeResult = docRef.update(updates);
  System.out.println("Update time : " + writeResult.get());
  // [END fs_delete_fields]
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:16,代碼來源:ManageDataSnippets.java

示例12: writeBatch

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
/** Write documents in a batch. */
void writeBatch() throws Exception {
  db.collection("cities").document("SF").set(new City()).get();
  db.collection("cities").document("LA").set(new City()).get();

  // [START fs_write_batch]
  // Get a new write batch
  WriteBatch batch = db.batch();
  // Set the value of 'NYC'
  DocumentReference nycRef = db.collection("cities").document("NYC");
  batch.set(nycRef, new City());

  // Update the population of 'SF'
  DocumentReference sfRef = db.collection("cities").document("SF");
  batch.update(sfRef, "population", 1000000L);

  // Delete the city 'LA'
  DocumentReference laRef = db.collection("cities").document("LA");
  batch.delete(laRef);

  // asynchronously commit the batch
  ApiFuture<List<WriteResult>> future = batch.commit();
  // ...
  // future.get() blocks on batch commit operation
  for (WriteResult result :future.get()) {
    System.out.println("Update time : " + result.getUpdateTime());
  }
  // [END fs_write_batch]
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:30,代碼來源:ManageDataSnippets.java

示例13: testAddDocWithAutoGenId

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
@Test
public void testAddDocWithAutoGenId() throws Exception {
  String autoId = manageDataSnippets.addDocumentDataWithAutoGeneratedId();
  City city = new City("Tokyo");
  city.setCountry("Japan");
  DocumentReference docRef = db.collection("cities").document(autoId);
  assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:9,代碼來源:ManageDataSnippetsIT.java

示例14: testAddDocAfterAutoGenId

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
@Test
public void testAddDocAfterAutoGenId() throws Exception {
  String autoId = manageDataSnippets.addDocumentDataAfterAutoGeneratingId();
  City city = new City();
  DocumentReference docRef = db.collection("cities").document(autoId);
  assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:8,代碼來源:ManageDataSnippetsIT.java

示例15: testUpdateSimpleDocument

import com.google.cloud.firestore.DocumentReference; //導入依賴的package包/類
@Test
public void testUpdateSimpleDocument() throws Exception {
  manageDataSnippets.updateSimpleDocument();
  DocumentReference docRef = db.collection("cities").document("DC");
  City city = new City("Washington D.C.");
  city.setCapital(true);
  assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
}
 
開發者ID:GoogleCloudPlatform,項目名稱:java-docs-samples,代碼行數:9,代碼來源:ManageDataSnippetsIT.java


注:本文中的com.google.cloud.firestore.DocumentReference類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。