1

I have a problem with my chat in firebase, I'm trying to find what chats my user is in, for doing that, I want to run a query that finds these id inside an array calls Conversadores inside my document which is inside my collection called mensajes.

Mensajes (collection) >> document >> conversadores (array of strings) 

Is it possible to do this? I'm ran out of ideas,

@Injectable()
 export class ChatProvider {
 private itemDoc: AngularFirestoreCollection<Mensajes>;
 item: Observable<Mensajes[]>;
 
      constructor(private afs: AngularFirestore, private _lp: LoginProvider) {
           this.itemDoc = afs.collection('mensajes', ref =>
           ref.where('conversadores', '==', _lp.userId) //userId is a string (ex.'ford0013')
           .orderBy('fechaCreacion', 'desc'));
 
           this.item = this.itemDoc.valueChanges();
  }
 }

here is a picture of my database structure:

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

Have look at this answer from Sam Stern:

How to query documents which contains a sent parameter in Firestore?

He explains that it is "better to store your data as a map of booleans". So you should structure the fields in your document as:

{
  conversadores:
    {ford0013: true, 
     ford0014: true
    },
  fechaCreation: ....
  ....
}

and query like

db.collection("mensajes").where("conversadores.ford0013", "==", true).get()

Now, if you want, in addition, to order by fechaCreation, this will not work, see the doc: https://firebase.google.com/docs/firestore/solutions/arrays

The doc mentions an approach for "queries on multiple fields" which would lead to a structure like the following, but I think this will not work in your case where you want to use orderBy after the where.

{
  conversadores:
    {ford0013: 1502144665, 
     ford0014: 1502144665
    },
  fechaCreation: ....
  ....
}
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • thank u a lot, if i cant order by a field after that, theres any chance of order it by the last modification of the object? – dario rodriguez Apr 25 '18 at 11:46
  • @dariorodriguez I don't think you can natively sort based on last modification. You have to create a dedicated field in your document. But the problem is that you will not be able to filter and sort on two different fields in a query, see https://firebase.google.com/docs/firestore/query-data/order-limit-data. You could however do the sorting in your front end if the result of your where query is not too big. By the way, would you mind to also upvote the answer, if you are satisfy with its quality, by clicking the arrow up next to the answer? thank you! – Renaud Tarnec Apr 25 '18 at 12:08
  • I really appreciate it, im alredy sorting in mi client. – dario rodriguez Apr 25 '18 at 12:16
  • btw i alredy upvote but i dont have enough reputation to be displayed as a public upvote :( – dario rodriguez Apr 25 '18 at 12:17
  • yes it is true you cannot upvote for the moment, sorry if I have insisted and for the inconvenience!! – Renaud Tarnec Apr 25 '18 at 12:21