目前,我有两个与Firebase相关的孩子,对话和帖子。
{
"conversations" : {
"-LFzccEzciNPSTFAZAhb" : {
"49C91D37EE1C4B3E07FE24FEBE9ED72B" : "true",
"CAD54A563CAB99107D9BBDB7F2234FA3" : "true",
"Date" : "2018-06-27 05:06:12 +0000",
"convoID" : "-LFzccEzciNPSTFAZAhb",
"created_at" : 1.5300759725991712E9,
"last_message" : "lesseee",
"last_message_time" : 1530077715525,
"postID" : "-LFzT4c6ylIcPne9F7QS",
"status" : "sent",
"timeOfDeletion" : 1530159609351
},
"-LFzd4rx4hCKtdls2yyF" : {
"49C91D37EE1C4B3E07FE24FEBE9ED72B" : "true",
"CAD54A563CAB99107D9BBDB7F2234FA3" : "true",
"Date" : "2018-06-27 05:08:13 +0000",
"convoID" : "-LFzd4rx4hCKtdls2yyF",
"created_at" : 1.530076093932972E9,
"last_message" : "feeling terrible",
"last_message_time" : 1530077516414,
"postID" : "-LFzd-uN4kRfU18TBj0N",
"status" : "sent",
"timeOfDeletion" : 1530162473644
}
},
和帖子:
"posts" : {
"-LFzT4c6ylIcPne9F7QS" : {
"Revealed" : "false",
"datePosted" : "2018-06-27 04:20:09 +0000",
"post" : "Hey boys",
"poster" : "49C91D37EE1C4B3E07FE24FEBE9ED72B",
"revealedDate" : 1530073209351,
"reveals" : 1,
"revealsRequired" : 13,
"timeOfDeletion" : 1530159609351,
"watchedBy" : {
"3ASP4M5mkTPGPO1qQhfVKXsr6Qf2" : "false",
"AGdUYuWFJ9dC4VcPKeQJoMOM1xe2" : "false",
"Hsm2R97Y1VZbhXHNrRvHuTKSrYv1" : "false",
"Ih5m9VUnJnewKvqiZCVgBFwCFrz1" : "false",
"N0IjBvmj9ieAKoBGpSJIitN6xmI3" : "false",
"NMo1gUPKWFcdhsrnCbKte7JfrcA2" : "false",
"NuAF78saM4OiaSlIVTtLD4gHWnp1" : "false",
"OclP0yKT9ig6TtifF2ik1yh2TRr2" : "false",
"XyF2qaRasbfNfwo6KM6ZClrsud42" : "false",
"bc7gjBues0XWimaq3AKiLYRKJzc2" : "false",
"dlwFYqlu2mgetB5zO6TNmFGBWcb2" : "false",
"h42bS6QbVEdUkNJv2yjrU7L09HX2" : "-LFzccEzciNPSTFAZAhb",
"i0U1oShLCWRVPezSzfUQl7VmwFB2" : "false",
"kAfSAHHUWNcCKRjR28EtuXclWmE3" : "false",
"mSWA8kF4XUMsK3fXDqx7iIBBJcb2" : "false",
"qNWbYTlfYvXOIhNNjFkvrgjXCOk1" : "false",
"yuUJHFcihgej6BFtCjXhL8cgLMC2" : "false"
}
},
每分钟,在数据库上运行一次cron作业,以删除旧帖子,例如:
exports.hourly_job =
functions.pubsub.topic('hourly-tick').onPublish((event) => {
const currentTime = Date.now()
const getPostsForDate = admin.database().ref('posts').orderByChild('timeOfDeletion').endAt(currentTime);
return getPostsForDate.once('value', (snapshot) => {
var updates = {};
snapshot.forEach((childSnapshot) => {
const postDetails = childSnapshot.val();
const postIDtoDelete = childSnapshot.key
updates[postIDtoDelete] = null
})
admin.database().ref('posts').update(updates)
})
});
在数据库中,如果用户开始与帖子的对话,则帖子子代中的watchedBy值将从“ false”更改为对话的ID。 cron作业每分钟都会运行一次,以删除旧帖子;但是,对话也需要删除。为了尽量减少数据下载使用量,我将会话ID与watchedBy节点相关联。现在,删除一个子级后,如何遍历watchedBy子级中的每个用户,查看该值是否不是“ false”,如果是,则删除具有该ID的对话?我需要执行此操作 ,而无需下载其他任何信息,而只是从子快照中访问数据。我也在尝试最小化数据下载使用量,那么这种设计是否可行且有效?
旧代码(在尝试最小化数据下载使用之前)
exports.hourly_job =
functions.pubsub.topic('hourly-tick').onPublish((event) => {
const getPostsForDate = admin.database().ref('posts').on('value', (snapshot) => {
const currentTime = Date.now()
snapshot.forEach((childSnapshot) => {
const postDetails = childSnapshot.val();
const timeOfPostDeletion = postDetails.timeOfDeletion
if (currentTime > timeOfPostDeletion) {
const postIDtoDelete = childSnapshot.key
const postDelete = admin.database().ref('/posts/'+ postIDtoDelete).remove()
const conversationsToRemove = admin.database().ref('/convoPosts/' + postIDtoDelete).on('value', (snapshot) => {
snapshot.forEach((childSnapshot) => {
const convoKey = childSnapshot.key
conversationReferenceDeletion = admin.database().ref('/conversations/' + convoKey).remove()
})
})
}
})
})
return true
});
编辑:尝试进行迭代。
<!-- language: node.js -->
const peopleWatching = postDetails.child('watchedBy').val()
updates[postIDtoDelete] = null
for (child in people) {
let value = child.val();
if (value !== "false") {
convoUpdates[value] = null
}
}
答案 0 :(得分:2)
请尝试以下操作:
return admin.database().ref(`/watchedBy`).once('value').then(snap => {
if (snap.hasChildren()) {
const updates = {};
snap.forEach(child => {
if(child.val() !== "false"){
updates[child.key] = null;
}
});
return p.update(updates);
}else{
return null;
}
});