Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

378
Visualizações
How to add Document with Custom ID to firestore in firestore 9

How to add a custom id to a firestore document instead of automatically generated id by firebase 9?

i've problem transforming this code into firebase 9 version -

db.collection("cities").doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA" })
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The example that you've provided seems to be from Firestore's documentation itself. Switch to (modular) tab instead of (namespaced) to see the same in V9 syntax.

enter image description here

The syntax for setting a document with custom ID using Modular SDK is follows (for the same example):

import { doc, setDoc } from "firebase/firestore"; 

await setDoc(doc(db, "cities", "LA"), {
  name: "Los Angeles",
  state: "CA",
  country: "USA"
});
about 4 years ago · Juan Pablo Isaza Relatório

0

after researching a lot of time I got a solution for this by myself

Try this one.

So, the solution is:

        FirebaseFirestore db = FirebaseFirestore.getInstance();

        // [START set_document]
        Map<String, Object> city = new HashMap<>();
        city.put("batch", "25");
        city.put("blood", ""+stuBlood.getText().toString());
        city.put("email", ""+stuEmail.getText().toString());
        city.put("facebook", ""+stuFacebook.getText().toString());
        city.put("gender", ""+stuGender.getText().toString());
        city.put("id", ""+stuID.getText().toString());
        city.put("image", ""+stuImage.getText().toString());
        city.put("location", ""+stuLocation.getText().toString());
        city.put("name", ""+stuName.getText().toString());
        city.put("phone", ""+stuPhone.getText().toString());
        city.put("whatsapp", ""+stuWhatsApp.getText().toString());
        city.put("telegram", ""+stuTelegram.getText().toString());
        city.put("session", "2020-2021");

        
        String documentID = "YOUR_CUSTOM_DOC_ID (You can also add String here)";

        db.collection("YOUR_COLLECTION_PATH").document(""+documentID)
                .set(city)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "DocumentSnapshot successfully written!");
                        Toast.makeText(add_student_info.this, "Added Successfully!", Toast.LENGTH_LONG).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w(TAG, "Error writing document", e);
                        Toast.makeText(add_student_info.this, "Failed to add Student data!", Toast.LENGTH_LONG).show();

                    }
                });
        // [END set_document]


    }

Hope it's working now

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda