Adding Server Timestamp field to the Object which being added

后端 未结 4 478
花落未央
花落未央 2020-12-28 08:32

I have a Challenge object, which has it\'s own properties and I\'m able to add it to the database successfully like this:

DocumentReference chal         


        
4条回答
  •  失恋的感觉
    2020-12-28 08:40

    Yes you can, using a Map. First of all, according to official docs it will be necessary to use an annotation that looks like this:

    @ServerTimestamp Date time;
    

    Annotation used to mark a Date field to be populated with a server timestamp. If a POJO being written contains null for a @ServerTimestamp-annotated field, it will be replaced with a server-generated timestamp.

    This is how you can update the latestUpdateTimestamp field with the server timestamp and the challangeId with the desired value at the same time.

    DocumentReference senderRef = challengeRef
        .document(loggedUserEmail)
        .collection("challenges_feed")
        .document(callengeID);
    
    Map updates = new HashMap<>();
    updates.put("latestUpdateTimestamp", FieldValue.serverTimestamp());
    updates.put("challangeId", "newChallangeId");
    senderRef.update(updates).addOnCompleteListener(new OnCompleteListener() {/* ... */}
    

提交回复
热议问题