Firebase Firestore中‘sentinel’含义解析及serverTimestamp相关疑问
Great question! In the context of Firebase Firestore, a sentinel is essentially a special placeholder value that tells the Firestore backend to perform a specific action or generate a dynamic value when the write operation (like set() or update()) is processed on the server side.
Take firebase.firestore.FieldValue.serverTimestamp() as the perfect example. When you pass this sentinel in a set() or update() call, you're not sending an actual timestamp value from your client. Instead, you're sending a signal to the Firestore server: "Hey, when you process this write, generate the current server time and put that here instead of this placeholder."
Sentinels aren't limited to timestamps either—Firestore provides other sentinel values for common write operations:
FieldValue.delete(): Tells the server to remove a specific field from the documentFieldValue.arrayUnion(): Instructs the server to add items to an array without creating duplicatesFieldValue.arrayRemove(): Tells the server to remove specific items from an array
Each of these works the same core way: they don't represent a literal value to store in the database. Instead, they're special markers that trigger server-side logic when the write is executed.
To directly answer your follow-up question: Yes, this sentinel absolutely acts as a placeholder, and the actual timestamp value is generated on the server side. This is far more reliable than using a client-side timestamp, since client device clocks can be inaccurate or out of sync.
内容的提问来源于stack exchange,提问作者Andy Fusniak

