如何使用WriteBatch从列表中移除项目?

huangapple go评论64阅读模式
英文:

How to remove item in list with WriteBatch?

问题

fireDB.document(groupPath).collection("users").whereArrayContains("cars", carPath).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
	@Override
	public void onComplete(@NonNull Task<QuerySnapshot> task) {
		if (task.isSuccessful() && task.getResult() != null) {
			QuerySnapshot snapshot = task.getResult();
			WriteBatch batch = fireDB.batch();  // Initialize WriteBatch
			
			for (DocumentSnapshot curr : snapshot.getDocuments()) {
				List<String> cars = (List<String>) curr.get("cars");
                if (cars == null) continue;
                
				if (cars.size() <= 2) {
					batch.delete(snapshot.getReference());
				} else {
					cars.remove(carPath);  // Remove the string from the list
					batch.update(curr.getReference(), "cars", cars);  // Update the document with modified list
				}
			}
			
			batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
				@Override
				public void onComplete(@NonNull Task<Void> batchTask) {
					if (batchTask.isSuccessful()) {
						// Batch write successful
					} else {
						// Batch write failed
					}
				}
			});
		}
	}
});
英文:

I'm using Firebase Cloud database. I queried a collection and got a list of documents. Each document contains a field cars which contains strings. In case the array contains at least three strings I want to remove the string carPath from this array (not the whole array). Otherwise, it should remove the whole document. I'm using WriteBatch. What I did:

fireDB.document(groupPath).collection(&quot;users&quot;).whereArrayContains(&quot;cars&quot;,carPath).get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
	@Override
	public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
		if (task.isSuccessful() &amp;&amp; task.getResult() != null) {
			QuerySnapshot snapshot = task.getResult();
			for (DocumentSnapshot curr : snapshot.getDocuments()) {
				List&lt;String&gt; cars = (List&lt;String&gt;) curr.get(&quot;cars&quot;);
                if (cars == null) continue;
				if (cars.size() &lt;= 2) {
					batch.delete(snapshot.getReference());
				} else {
					// What to do here?
				}
			}
		}
	}
});

How should I remove one item in the list with WriteBatch?

答案1

得分: 1

你所尝试做的只是一个文档更新。你可以使用FieldValue.arrayRemove()进行常规更新。唯一不同的是,你会在批处理的上下文中使用update(),而不是独立的更新。

batch.update(snapshot.getReference(), "cars", FieldValue.arrayRemove(carPath));

英文:

What you're trying to do is just a document update. You can do a regular update with FieldValue.arrayRemove(). Except you will do it in the context of a batch using update() instead of a standalone update.

batch.update(snapshot.getReference(), &quot;cars&quot;, FieldValue.arrayRemove(carPath));

huangapple
  • 本文由 发表于 2020年9月15日 06:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63892468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定