I am trying to update a field in a subcollection every 2 minutes. I am changing the field "Status" to "Off" if the timestamp is older than 3 minutes old. I have this code that someone helped me with and It works with a collection but I dont know what collection path to use to access all documents that have this subcollection in them.
After 5 minutes I want the "Status" Field to be "Off" if the timestamp is more than 3 minutes old.
const snap = await db.collection("MX") works but only for 1 layer deep. Each document in userProfiles has a subCollection of MX.
I have tried wildcards and several different collection paths.
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')
});
EDIT: userProfile -> each doc -> 4 sub collections (MX, employees, settings, logs)
I have documents stored inside of MX. These documents will have a Status and a Timestamp.
I want this pub/sub to check all of the documents stored inside of the MX subcollection.
And if the timestamp is 3-5 minutes old, and Status = On. I want to change the Status to Off
So I am checking all MX subcollections. In this case there is 4 documents in userProfiles and there is a MX subcollection inside each document.
I appreciate any and all help. I hope I am explaining this well enough. Thanks.
MX
collection that have anMX
sub-collection – Renaud TarnecMX
subcollection of auserProfile
that you want to update? Or you want to update all docs of thisuserProfile/MX
subcollection? – Renaud Tarnec