Home > Back-end >  FireBase Firestore storing data in realtime firestore for same COLLECTION id
FireBase Firestore storing data in realtime firestore for same COLLECTION id

Time:05-21

I have a form which insert record with multiple values for userID logged in .

Whenever I am submitting a second time form ,its updating existing record rather than inserting new record .

I need to be able to insert multiple records for same documentID(collectionID) rather than updating existing record

 String uniqueID = UUID.randomUUID().toString();
DocumentReference documentReference = fStore.collection("data").document(userID);
Map<String, Object> user = new HashMap<>();
user.put("date", dates);
user.put("vehicle_number", vehicle_number);
user.put("cost", costs);
user.put("liters", liters);
user.put("fuel_type", sp);
user.put("ID", uniqueID);
 documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
      
        Toast.makeText(adding_vehicle_expense.this, "Data inserted", Toast.LENGTH_SHORT).show();

    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Log.d("here2"   TAG, "onFailure: "   e.toString());
    }
});

Firebase enter image description here

enter image description here

CodePudding user response:

There is no way to have multiple documents directly inside another document. But it is possible to have a subcollection under a document, and then add multiple documents in that subcollection by calling add().

E.g.

documentReference.collection("userdata").add(user).addOnSuccessListener(new OnSuccessListener<Void>() {
  ...
  • Related