Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query firestore array of objects

I have firestore collection events.

Every event have date which is array of objects with two strings, startTime and endTime.

enter image description here

Is it possibile to retrive every events which have at list one startTime which is bigger than current time?

like image 968
Ivan Tanasijevic Avatar asked May 30 '18 15:05

Ivan Tanasijevic


2 Answers

This is not possible with your document structure. You can't target array elements in queries. Instead, you may want to store the max startTime as a top-level field in the document, and use that instead in your queries instead.

Duplicating data like this to suit your queries is common. With NoSQL databases, the structure of your data should always follow the queries you intend to make.

like image 198
Doug Stevenson Avatar answered Sep 22 '22 09:09

Doug Stevenson


You can get the whole document by querying firebase with "array-contains", but you must search for the exact properties of the object and the exact object itself. In your case "endTime" and "startTime".

firebase.firestore.collection('events').where("date", "array-contains", {endTime: "March 18 ....", startTime: "March 19 ...."})
like image 43
Pipera Avatar answered Sep 23 '22 09:09

Pipera