由于 cloud firestore 中没有“移动”或“重命名”命令,因此我每天早上 6 点(英国时间)尝试将所有文档从一个集合复制到另一个集合。到目前为止,我已经想出了这段代码,但不确定如何获取 resID,然后将其保存在另一个集合中后删除原始文档:
exports.scheduledFunctionCrontab = functions.pubsub.schedule('0 6 * * *')
.timeZone('Europe/London') // Is this correct?
.onRun((context) => {
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
console.log(`This function ran at ${Date.now}!`);
db.collection("Restaurants/${**resID**}/openorders").get().then(query => {
query.forEach(function(doc) {
var promise = firestore.collection("Restaurants/${**resID**}/closed/${year}/${month}/${day}/closed).doc(doc.data().orderID).set(doc.data());
// How I delete this document here?
});
})
.catch((error)=>{
console.log('Failure:', e);
console.log(`This function ran with ERROR at ${Date.now}!`);
});
return null;
});
答案 0 :(得分:1)
要遍历所有名为 openorders
的集合,您可以使用 collection group query。
要获取每个 openorders
文档的父文档,您可以使用 ref
和 parent
属性。
比如:
db.collectionGrouo("openorders").get().then(query => {
query.forEach(function(doc) {
let resID = doc.ref.parent.parent.id;
...
});
})
对于这种类型的事情,我总是建议将参考文档放在手边,以 DocumentSnapshot
开头。