Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore query by Object Key

I have a series of nested objects in a collection in firestore, that have a key of a UID. I want to query all documents that relate to a user, by checking if their UID is the object key (basically if it exists). This is the model in firestore: enter image description here

and this is how I am currently trying to return the documents:

  getAllTenancyOffersByUserId() {
    return this.afs.collection('collection_name', ref =>
      ref.where(`application.members.${this._auth.currentUserId}`, '==', !null),
    );
  }

but this returns nothing. I understand that I could just query by that status, however it is constantly updating so will cause problems

like image 248
Jm3s Avatar asked Jan 30 '19 11:01

Jm3s


3 Answers

The following should do the trick:

  getAllTenancyOffersByUserId() {
    return this.afs.collection('members', ref =>
      ref.where(firebase.firestore.FieldPath.documentId(), '==', `${this._auth.currentUserId}`)
    );
  }

See the corresponding doc: https://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#.documentId

like image 58
Renaud Tarnec Avatar answered Nov 15 '22 12:11

Renaud Tarnec


Actually, what you could do is to filter by any string field that you have in the object like this:

.collection("collectionName")
.where("mapArray.map.stringField", ">", "''")

the important part here is the string field being; ">", "''"

like image 37
ctwhome Avatar answered Nov 15 '22 10:11

ctwhome


I believe it is not possible to query whether a object exists or not. I think your idea of querying a field inside your object may be the best bet.

What you could try is querying the date_modified field for anything greater than 1900.

getAllTenancyOffersByUserId() {
  return this.afs.collection('collection_name', ref =>
    ref.where(`
      application.members.${this._auth.currentUserId}.date_modified`, 
      '>=',
      new Date('1900-01-01')
    ),
  );
}
like image 39
Matthew Mullin Avatar answered Nov 15 '22 11:11

Matthew Mullin