Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

android - How do I use document reference to add field (prevent overwriting) in my document in Firestore?

First of all, I hope anyone could help adjust my code line using document reference as below.

As you can see from my firebase console below, my FaceEmotion-id document has Faceemotion and Timestamp field. Using my codes below, everytime user gets different emotion, the firestore will overwrite the emotions. Hence I couldnt see what are the different emotions user get when using my app and the different time he/she has used my app.

I need to prevent overwriting for the two fields mentioned above. Please help me. What I want is to add fields in the document not overwriting it.

enter image description here

One more, I have no idea how to generate different id for the document named FaceEmotion-id. I actually had referenced the document beforehand using Uid as you can see from my code below. But for the subdocument in the "Result" collection. I cant seem to find a way to make it unique.

This is my codes:

public static final String TAG = "TAG";
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
String FaceEmotion;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detection);
  
    fAuth = FirebaseAuth.getInstance();
    fStore = FirebaseFirestore.getInstance();

    StartQues = (Button) findViewById(R.id.view_question);
    StartQues.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String Emotion = EmotionType.toString();

            userID = fAuth.getCurrentUser().getUid();
            FaceEmotion = "FaceEmotion";
            DocumentReference documentReference = fStore.collection("users").document(userID).collection("Result").document("FaceEmotion-id");
            //DocumentReference documentReference = fStore.collection("users").document(userID).collection("FaceEmotion").document(Emotion);
            Map<String,Object> user = new HashMap<>();
            user.put("FaceEmotion",Emotion);
            user.put("timestamp", FieldValue.serverTimestamp());

            documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "onSuccess: Data has been saved "+ userID);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d(TAG, "onFailure: " + e.toString());
                }
            });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It sounds like you want to store a list of timestamps and emojis. If you want to store those in a single document, the logical data structure would be to store them in an array/set with something like this:

user.put("FaceEmotion",Emotion);
user.put("timestamp", FieldValue.serverTimestamp());

documentReference.update("emotions", FieldValue.arrayUnion(user))

Every time this code runs, it will add a new item to the emotions array in the document.

Also see the Firebase documentation on updating elements in an array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...