Reputation: 163
If any single doc exist in myCollection
in which array contains any value of arr array then I do not wants to add new document in myCollection2
I want to run this query on cloud function.
firestore structure
myCollection
myDoc1
array:[1,2,3]
myDoc2
array:[5,8,7]*
myCollection2 ....
How I am doing it
const arr=[2,5,8,9];
return db.runTransaction(async t=>{
for(let i=0,i<arr.length<t++){
await t.get(db.collection('myCollection').where('array','array-contains'arr[i]).then(doc=>{
if(doc.exists) throw new functions.http.httpError('internal','not available');
})
}
return db.collection('mycollection2').add({
//fields
});
});
is this right approach?
Upvotes: 0
Views: 397
Reputation: 660
There is no way to do that. What you do is combine different queries which match your terms and combine them. Here is an example using rxjs and typescript
function getData(value1, value2)
{
const scenario1 = this.afs.collection(`collection1`, ref => {
let query: firebase.firestore.CollectionReference | f
firebase.firestore.Query = ref;
query = query.where("field1", "==", value1);
return query;
}).snapshotChanges()
.pipe(take(1))
.pipe(
map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as CustomDataType;
data.docId = a.payload.doc.id;
return data;
})
})
);
const scenario2 = this.afs.collection(`collection1`, ref => {
let query: firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
query = query.where("field1", "==", value2);
return query;
}).snapshotChanges()
.pipe(take(1))
.pipe(
map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as CustomDataType;
data.docId = a.payload.doc.id;
return data;
})
})
);
return forkJoin(scenario1,scenario2)
.pipe(map((arr) => [...arr[0],...arr[1]] ));
}
Upvotes: 1