مثالهای زیر نحوه حذف اسناد، فیلدها و مجموعهها را نشان میدهند.
اسناد را حذف کنید
برای حذف یک سند، از متد delete()
استفاده کنید:
Web version 9
import { doc, deleteDoc } from "firebase/firestore"; await deleteDoc(doc(db, "cities", "DC"));
Web version 8
db.collection("cities").doc("DC").delete().then(() => { console.log("Document successfully deleted!"); }).catch((error) => { console.error("Error removing document: ", error); });
سریع
db.collection("cities").document("DC").delete() { err in if let err = err { print("Error removing document: \(err)") } else { print("Document successfully removed!") } }
هدف-C
[[[self.db collectionWithPath:@"cities"] documentWithPath:@"DC"] deleteDocumentWithCompletion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Error removing document: %@", error); } else { NSLog(@"Document successfully removed!"); } }];
Java
db.collection("cities").document("DC") .delete() .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Log.d(TAG, "DocumentSnapshot successfully deleted!"); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "Error deleting document", e); } });
Kotlin+KTX
db.collection("cities").document("DC") .delete() .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully deleted!") } .addOnFailureListener { e -> Log.w(TAG, "Error deleting document", e) }
Dart
db.collection("cities").doc("DC").delete().then( (doc) => print("Document deleted"), onError: (e) => print("Error updating document $e"), );
جاوا
پایتون
Python
C++
db->Collection("cities").Document("DC").Delete().OnCompletion( [](const Future<void>& future) { if (future.error() == Error::kErrorOk) { std::cout << "DocumentSnapshot successfully deleted!" << std::endl; } else { std::cout << "Error deleting document: " << future.error_message() << std::endl; } });
Node.js
برو
PHP
$db->collection('samples/php/cities')->document('DC')->delete();
وحدت
DocumentReference cityRef = db.Collection("cities").Document("DC"); cityRef.DeleteAsync();
سی شارپ
DocumentReference cityRef = db.Collection("cities").Document("DC"); await cityRef.DeleteAsync();
یاقوت سرخ
هنگامی که یک سند را حذف می کنید، Cloud Firestore به طور خودکار اسناد موجود در زیر مجموعه های خود را حذف نمی کند. همچنان می توانید با مراجعه به اسناد زیر مجموعه دسترسی داشته باشید. برای مثال، میتوانید در مسیر /mycoll/mydoc/mysubcoll/mysubdoc
به سند دسترسی داشته باشید، حتی اگر سند اجداد را در /mycoll/mydoc
کنید.
اسناد اجدادی که وجود ندارند در کنسول ظاهر می شوند ، اما در نتایج پرس و جو و عکس های فوری ظاهر نمی شوند.
اگر می خواهید یک سند و تمام اسناد موجود در زیر مجموعه های آن را حذف کنید، باید این کار را به صورت دستی انجام دهید. برای اطلاعات بیشتر، به حذف مجموعه ها مراجعه کنید.
حذف فیلدها
برای حذف فیلدهای خاص از یک سند، از FieldValue.delete()
هنگام به روز رسانی یک سند استفاده کنید:
Web version 9
import { doc, updateDoc, deleteField } from "firebase/firestore"; const cityRef = doc(db, 'cities', 'BJ'); // Remove the 'capital' field from the document await updateDoc(cityRef, { capital: deleteField() });
Web version 8
var cityRef = db.collection('cities').doc('BJ'); // Remove the 'capital' field from the document var removeCapital = cityRef.update({ capital: firebase.firestore.FieldValue.delete() });
سریع
db.collection("cities").document("BJ").updateData([ "capital": FieldValue.delete(), ]) { err in if let err = err { print("Error updating document: \(err)") } else { print("Document successfully updated") } }
هدف-C
[[[self.db collectionWithPath:@"cities"] documentWithPath:@"BJ"] updateData:@{ @"capital": [FIRFieldValue fieldValueForDelete] } completion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Error updating document: %@", error); } else { NSLog(@"Document successfully updated"); } }];
Java
DocumentReference docRef = db.collection("cities").document("BJ"); // Remove the 'capital' field from the document Map<String,Object> updates = new HashMap<>(); updates.put("capital", FieldValue.delete()); docRef.update(updates).addOnCompleteListener(new OnCompleteListener<Void>() { // ... // ...
Kotlin+KTX
val docRef = db.collection("cities").document("BJ") // Remove the 'capital' field from the document val updates = hashMapOf<String, Any>( "capital" to FieldValue.delete() ) docRef.update(updates).addOnCompleteListener { }
Dart
final docRef = db.collection("cities").doc("BJ"); // Remove the 'capital' field from the document final updates = <String, dynamic>{ "capital": FieldValue.delete(), }; docRef.update(updates);
جاوا
پایتون
Python
C++
DocumentReference doc_ref = db->Collection("cities").Document("BJ"); doc_ref.Update({{"capital", FieldValue::Delete()}}) .OnCompletion([](const Future<void>& future) { /*...*/ });
Node.js
برو
PHP
$cityRef = $db->collection('samples/php/cities')->document('BJ'); $cityRef->update([ ['path' => 'capital', 'value' => FieldValue::deleteField()] ]);
وحدت
DocumentReference cityRef = db.Collection("cities").Document("BJ"); Dictionary<string, object> updates = new Dictionary<string, object> { { "Capital", FieldValue.Delete } };
سی شارپ
DocumentReference cityRef = db.Collection("cities").Document("BJ"); Dictionary<string, object> updates = new Dictionary<string, object> { { "Capital", FieldValue.Delete } }; await cityRef.UpdateAsync(updates);
یاقوت سرخ
حذف مجموعه ها
برای حذف کل یک مجموعه یا زیر مجموعه در Cloud Firestore، تمام اسناد موجود در مجموعه یا زیر مجموعه را بازیابی کرده و آنها را حذف کنید. اگر مجموعههای بزرگتری دارید، ممکن است بخواهید اسناد را در دستههای کوچکتر حذف کنید تا از خطاهای خارج از حافظه جلوگیری کنید. این فرآیند را تا زمانی که کل مجموعه یا زیر مجموعه را حذف نکنید، تکرار کنید.
حذف یک مجموعه مستلزم هماهنگی تعداد نامحدودی از درخواست های حذف فردی است. اگر نیاز به حذف کل مجموعه دارید، این کار را فقط از یک محیط سرور قابل اعتماد انجام دهید. در حالی که امکان حذف مجموعه از یک سرویس گیرنده موبایل/وب وجود دارد، انجام این کار پیامدهای امنیتی و عملکردی منفی دارد.
تکههای زیر تا حدودی ساده شدهاند و با مدیریت خطا، امنیت، حذف زیر مجموعهها یا به حداکثر رساندن عملکرد سروکار ندارند. برای کسب اطلاعات بیشتر در مورد یک روش توصیه شده برای حذف مجموعه ها در تولید، به حذف مجموعه ها و زیر مجموعه ها مراجعه کنید.
وب
// Deleting collections from a Web client is not recommended.
سریع
// Deleting collections from an Apple client is not recommended.
هدف-C
// Deleting collections from an Apple client is not recommended.
Java
// Deleting collections from an Android client is not recommended.
Kotlin+KTX
// Deleting collections from an Android client is not recommended.
Dart
حذف مجموعه ها از مشتری توصیه نمی شود.
جاوا
پایتون
Python
C++
// This is not supported. Delete data using CLI as discussed below.
Node.js
برو
PHP
function data_delete_collection(string $projectId, string $collectionName, int $batchSize) { // Create the Cloud Firestore client $db = new FirestoreClient([ 'projectId' => $projectId, ]); $collectionReference = $db->collection($collectionName); $documents = $collectionReference->limit($batchSize)->documents(); while (!$documents->isEmpty()) { foreach ($documents as $document) { printf('Deleting document %s' . PHP_EOL, $document->id()); $document->reference()->delete(); } $documents = $collectionReference->limit($batchSize)->documents(); } }
وحدت
// This is not supported. Delete data using CLI as discussed below.
سی شارپ
private static async Task DeleteCollection(CollectionReference collectionReference, int batchSize) { QuerySnapshot snapshot = await collectionReference.Limit(batchSize).GetSnapshotAsync(); IReadOnlyList<DocumentSnapshot> documents = snapshot.Documents; while (documents.Count > 0) { foreach (DocumentSnapshot document in documents) { Console.WriteLine("Deleting document {0}", document.Id); await document.Reference.DeleteAsync(); } snapshot = await collectionReference.Limit(batchSize).GetSnapshotAsync(); documents = snapshot.Documents; } Console.WriteLine("Finished deleting all documents from the collection."); }
یاقوت سرخ
با Firebase CLI داده ها را حذف کنید
همچنین می توانید از Firebase CLI برای حذف اسناد و مجموعه ها استفاده کنید. برای حذف داده ها از دستور زیر استفاده کنید:
firebase firestore:delete [options] <<path>>
اطلاعات را با کنسول حذف کنید
میتوانید اسناد و مجموعهها را از صفحه Cloud Firestore در کنسول حذف کنید . با حذف یک سند از کنسول، تمام داده های تودرتو در آن سند، از جمله هر مجموعه فرعی، حذف می شود.