如何将新的“Collection名称”单独添加到Firestore中。

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

How to add new Collection name alone to Firestore

问题

以下是您要翻译的内容:

我想在Firestore中添加新的集合名称,而不需要使用POJO类。当我尝试使用POJO类时,其中会多出一个字段名称。我不想要添加那个字段。我尝试过使用@IgnoreExtraProperties,但无法避免添加这些字段。在Firestore和Android中是否有其他方法可以做到这一点。提前感谢您。

我的代码:

private void setNewCategory(String downloadUrl){
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    DocumentReference newMainCatRef = db
            .collection("HomeFeed")
            .document("5HEkE0ac7sMa7Gjnvf3E")
            .collection("MainCategory")
            .document();
    itemId = newMainCatRef.getId();
    MainCategory category = new MainCategory();

    category.setCategory_id(itemId);
    category.setCategory_name(category_name.getText().toString());
    category.setCategory_url(downloadUrl);
    category.setPriority(priority.getValue());

    newMainCatRef.set(category).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d(TAG, "onSuccess: 更新新字段到cat成功");
            FirebaseFirestore NC = FirebaseFirestore.getInstance();
            CollectionReference NewCategory = NC
                    .collection("Categories")
                    .document("tUdFCajDcQT995jX6G4k")
                    .collection(category_name.getText().toString());
            CartItem cartItem = new CartItem();

            NewCategory.document(itemId).set(cartItem).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(CategoryUpload.this, "添加ID成功", Toast.LENGTH_SHORT).show();
                }
            });
        }
    });
}

我的PoJo类:

@IgnoreExtraProperties
public class CartItem {

    private String name;

    public CartItem() {}

    public CartItem(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

其中name字段被添加了

英文:

I wanted to add new collection name to firestore without POJO class .When i tried this with pojo class I am getting extra field name in it. i don't want that to be added. i tried using @IgnoreExtraProperties but couldn't avoid adding the fields . is there any other way to do it new to firestore and android .Thanks in advance.
My code:

private void setNewCategory(String downloadUrl){
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference newMainCatRef = db
.collection(&quot;HomeFeed&quot;)
.document(&quot;5HEkE0ac7sMa7Gjnvf3E&quot;)
.collection(&quot;MainCategory&quot;)
.document();
itemId = newMainCatRef.getId();
MainCategory category = new MainCategory();
category.setCategory_id(itemId);
category.setCategory_name(category_name.getText().toString());
category.setCategory_url(downloadUrl);
category.setPriority(priority.getValue());
newMainCatRef.set(category).addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, &quot;onSuccess: Success on Updating the new Field to cat&quot;);
FirebaseFirestore NC = FirebaseFirestore.getInstance();
CollectionReference NewCategory = NC
.collection(&quot;Categories&quot;)
.document(&quot;tUdFCajDcQT995jX6G4k&quot;)
.collection(category_name.getText().toString());
CartItem cartItem = new CartItem();
NewCategory.document(itemId).set(cartItem).addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(CategoryUpload.this, &quot;succced with adding ID&quot;, Toast.LENGTH_SHORT).show();
}
});

My PoJo class:

@IgnoreExtraProperties
public class CartItem {
private String name;
public CartItem() {}
public CartItem(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

where the name field is getting added

答案1

得分: 1

从您的问题中并不十分清楚您试图实现什么。听起来您可能正在尝试创建一个空的集合。然而,在Firestore中实际上是不可能的。没有创建空集合的操作。当向集合中写入文档时,集合会自动出现;当最后一个文档被移除时,它们会自动删除。

对一个不存在的集合进行查询将返回0个文档 - 不会出现错误。因此,空集合和根本不存在的集合之间实际上没有区别。您的代码应该假设无论是否在控制台中可见,都可以读写集合。

英文:

It's not terribly clear from your question what you're trying to achieve. It sounds like maybe you're trying to create an empty collection. However, that is not actually possible in Firestore. There is no operation to create an empty collection. Collections automatically spring into existence when a document is written to them, and they are automatically removed when the last document is removed.

A query against a collection that doesn't exist will just yield 0 documents - there will be no error. As such, there really no difference between an empty collection, and a collection that doesn't exist at all. Your code should just assume that it can read and write a collection regardless of whether or not you can see it in the console.

huangapple
  • 本文由 发表于 2020年4月7日 20:10:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61079749.html
匿名

发表评论

匿名网友

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

确定