Tal Barda
Tal Barda

Reputation: 4287

Adding Server Timestamp field to the Object which being added

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 challengeRef=usersRef.document(loggedUserEmail).collection("challenges_feed").
                document(callengeID);
challengeRef.set(currentChallenge);

This is how it looks like in the database:

enter image description here

I'd like to make a new field in the database (under this challenge) which called latestUpdateTimetamp. This is how it should look like (I have added it manually):

enter image description here

I have tried to set it in the constructor of the object like this:

private Map<String,String> latestUpdateTimestamp;

public Challenge(String id, String senderName,  String senderEmail) {   
            this.senderName=senderName;
            this.senderEmail = senderEmail;

            this.latestUpdateTimestamp= ServerValue.TIMESTAMP;
        }

But this is what I get in the database:

enter image description here

I'm trying to add the latestUpdateTimestamp to the Challenge and the Challenge object itself to the database at the same call. Is it possible?

Can I somehow add this timestamp as a property to this object before adding it?

I know I'm able to make a new call and add this field, but I'm wondering if it's possible at once.

Upvotes: 18

Views: 24614

Answers (4)

Alex Mamo
Alex Mamo

Reputation: 139019

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<String, Object> updates = new HashMap<>();
updates.put("latestUpdateTimestamp", FieldValue.serverTimestamp());
updates.put("challengeId", "newChallangeId");
senderRef.update(updates).addOnCompleteListener(new OnCompleteListener<Void>() {/* ... */}

Upvotes: 48

Vahe Aslanyan
Vahe Aslanyan

Reputation: 126

So you could use Firebase tools to do so.

Basically if you use @ServerTimestamp in your object and pass nil as a value then the server will automatically add a server timestamp.

Not sure how to do it in Java as I am a Swift developer, but just to give you an example this is what I added in my object model:

//This is a Swift code for demonstration
@ServerTimestamp var timeStamp: Timestamp?

So here it just says there is a variable named timeStamp of an optional Timestamp data type and there is no value assigned so it is nil.

And then I upload the object as I would do normally without passing any value with timeStamp.

This means it is uploaded as nil and Firestore automatically assigns it a server timestamp.

Upvotes: 1

Gast&#243;n Saill&#233;n
Gast&#243;n Saill&#233;n

Reputation: 13159

Kotlin

In kotlin we need to define an initialized data class to send the whole object to Firestore, make sure that you add your @ServerTimeStamp in the last field, since if you add it at first, the construction of your object will need to pass this time as a value to your Object instance.

data class Order(val orderId:String, val cart:MutableList<Cart>, @ServerTimeStamp timestamp:Date? = null)

Create your object and send the object values to Firestore

val order = Order("kJKLkj259ajHHkl",cartList)

FirebaseFirestore.getInstance().collection("orders").add(order)...

This will create your object with orderId, cartlist and also the timestamp which will be stored as

enter image description here

In my case I'm from Argentina, and the date picked up from my app was this one

timestamp: 30 de marzo de 2020, 10:12:43 UTC-3

You can find more about date formats here and here

Upvotes: 3

Masum
Masum

Reputation: 4969

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

Java

DocumentReference docRef = db.collection("objects").document("some-id");
Map<String,Object> post = new HashMap<>();
post.put("timestamp", FieldValue.serverTimestamp());

docRef.add(updates).addOnCompleteListener(new OnCompleteListener<Void>() {
 .....
}

Kotlin

val docRef = db.collection("objects").document("some-id")
val updates = HashMap<String, Any>()
updates["timestamp"] = FieldValue.serverTimestamp()

docRef.add(updates).addOnCompleteListener { }

Upvotes: 4

Related Questions