How to add new Collection name alone to Firestore

Shiva s :

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("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();
                }
            });

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

Doug Stevenson :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=477626&siteId=1