我试图每 2 分钟更新一次子集合中的字段。如果时间戳早于 3 分钟,我会将“状态”字段更改为“关闭”。我有有人帮助我编写的代码,它适用于集合,但我不知道使用什么集合路径来访问包含此子集合的所有文档。
5 分钟后,如果时间戳超过 3 分钟,我希望“状态”字段为“关闭”。
const snap = wait db.collection("MX") 有效,但仅适用于 1 层深度。 userProfiles 中的每个文档都有一个 MX 的子集合。
我尝试过通配符和几种不同的收集路径。
exports.updateIdle = functions.pubsub.schedule('every 2 minutes').onRun(async () => {
const db = admin.firestore();
// this is what line I am trying to figure out
const snap = await db.collection("userProfiles")
.where("Status", "=", "On")
.where("Timestamp", "<", admin.firestore.Timestamp.fromMillis(Date.now() - 3 * 60000))
.get();
await Promise.all(snap.docs.map(doc => doc.ref.update({Status: 'Off'})));
console.log('this goes off every two minutes')
});
编辑: userProfile -> 每个文档 -> 4 个子集合(MX、员工、设置、日志)
我的文档存储在 MX 中。这些文档将有一个状态和一个时间戳。
我希望此发布/订阅检查 MX 子集合中存储的所有文档。
如果时间戳是 3-5 分钟前的,并且状态 = 开启。我想将状态更改为关闭
所以我正在检查所有 MX 子集合。在本例中,userProfiles 中有 4 个文档,每个文档内都有一个 MX 子集合。
我感谢任何和所有的帮助。我希望我能很好地解释这一点。谢谢。
最佳答案
如果您想“更新任何具有旧时间戳的 MX 子集合中的所有文档”,则以下操作应该有效,使用 Promise.all()
:
exports.updateIdle = functions.pubsub.schedule('every 2 minutes').onRun(async () => {
const db = admin.firestore();
const querySnapshot = await db.collection("userProfiles")
.where("Status", "=", "On")
.where("Timestamp", "<", admin.firestore.Timestamp.fromMillis(Date.now() - 3 * 60000))
.get();
const promises = [];
querySnapshot.forEach(doc => {
const docRef = doc.ref;
promises.push(docRef.collection('MX').get());
});
const snapshotArrays = await Promise.all(promises);
// snapshotArrays contains an array of arrays of QueryDocumentSnapshots
const promises1 = [];
snapshotArrays.forEach(snapArray => {
snapArray.forEach(snap => {
promises1.push(snap.ref.update({
Status: "Off"
}))
})
});
return Promise.all(promises1);
});
<小时/>
当然,我假设您查找带有“旧时间戳”的 userProfile
文档的初始查询是正确的。
根据您的评论进行更新
据我了解,“时间戳已过 3-5 分钟,且状态 = 打开”的过滤是在 MX 文档级别完成的。然后,以下内容应该可以工作(我们只需将 where
子句移至 MX
集合)。但请注意,在本例中,我们读取了所有 userProfile
文档(每次读取都需要读取一个标准文档)。
exports.updateIdle = functions.pubsub.schedule('every 2 minutes').onRun(async () => {
const db = admin.firestore();
const querySnapshot = await db.collection("userProfiles").get();
const promises = [];
querySnapshot.forEach(doc => {
const docRef = doc.ref;
promises.push(docRef.collection('MX').where("Status", "=", "On")
.where("Timestamp", "<", admin.firestore.Timestamp.fromMillis(Date.now() - 3 * 60000)).get());
});
const snapshotArrays = await Promise.all(promises);
// snapshotArrays contains an array of arrays of QueryDocumentSnapshots
const promises1 = [];
snapshotArrays.forEach(snapArray => {
snapArray.forEach(snap => {
promises1.push(snap.ref.update({
Status: "Off"
}))
})
});
return Promise.all(promises1);
});
<小时/>
如果仍然遇到错误,请尝试不使用 where 子句,如下所示,并重新引用 where 子句以获得所需的 MX 文档选择。
//....
const querySnapshot = await db.collection("userProfiles").get();
const promises = [];
querySnapshot.forEach(doc => {
const docRef = doc.ref;
promises.push(docRef.collection('MX').get());
});
//....
关于javascript - Google Pub/Sub 更新 Firestore 子集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59142187/