【发布时间】:2023-03-09 12:44:01
【问题描述】:
有谁知道如何限制数组,以便在同一次写入中推入新项目并丢弃旧项目?
我猜这是不可能的,但它肯定会很方便。
// * Store notification
// Users collection
const usersCollection = db.collection('users').doc(uid).collection('notifications').doc();
// Write this notification to the database as well
await usersCollection.update({
count: admin.firestore.FieldValue.increment,
notifications: admin.firestore.FieldValue.arrayUnion({
'symbol': symbol,
'companyname': companyname,
'change': priceDifference,
'changeDirection': directionOperatorHandler,
'updatedPrice': symbolLatestPrice,
'timestamp': currentTimestamp,
})
});
用打字稿编写
或者,我正在考虑每周运行一个预定的云函数,以根据时间戳检查和修剪数组。
我使用数组来存储通知的原因是因为我期待大量的写入。
【问题讨论】:
-
如果您从客户端运行它,您可以制定一个查找计数值的安全规则,也可以创建一个云函数,在更改时触发并保留计数值,如果您是在服务器端,你可以通过对计数值的 if 语句自己实现。
-
谢谢安德烈,我会尝试 Doug 的建议,如果它不能满足我的需求,我可能会使用预定的云功能????
标签: firebase google-cloud-firestore google-cloud-functions