繁体   English   中英

如何将新的集合名称单独添加到 Firestore

[英]How to add new Collection name alone to Firestore

我想在没有 POJO class 的情况下向 Firestore 添加新的集合名称。当我使用 pojo class 尝试此操作时,我在其中获得了额外的字段名称。 我不希望添加它。 我尝试使用@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: Success on Updating the new Field to 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, "succced with adding ID", Toast.LENGTH_SHORT).show();
                }
            });

我的 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;
    }
}

添加名称字段的位置

从您的问题中,您要达到的目标并不是很清楚。 听起来您可能正在尝试创建一个空集合。 但是,这在 Firestore 中实际上是不可能的。 没有创建空集合的操作。 Collections 在写入文档时自动将 spring 变为存在,并在删除最后一个文档时自动删除。

针对不存在的集合的查询只会产生 0 个文档 - 不会有错误。 因此,空集合和根本不存在的集合之间确实没有区别。 您的代码应该假设它可以读取和写入集合,无论您是否可以在控制台中看到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM