Adding Server Timestamp field to the Object which being added

后端 未结 4 482
花落未央
花落未央 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:54

    As per Google Documentation You can use FieldValue.serverTimestamp(). Something like this

    Java

    DocumentReference docRef = db.collection("objects").document("some-id");
    Map post = new HashMap<>();
    post.put("timestamp", FieldValue.serverTimestamp());
    
    docRef.add(updates).addOnCompleteListener(new OnCompleteListener() {
     .....
    }
    

    Kotlin

    val docRef = db.collection("objects").document("some-id")
    val updates = HashMap()
    updates["timestamp"] = FieldValue.serverTimestamp()
    
    docRef.add(updates).addOnCompleteListener { }
    

提交回复
热议问题