3

I'm trying to write a nodejs cloud function to fetch a random document from my 'profiles' collection in firestore like this:

randomProfile = await admin.firestore().collection('profiles')
.where('fieldA', '==', ...)
.where('fieldB', '==', ...)
.where('fieldC', '==' ...)
.where('fieldD', '==',...)
.where('fieldE', '<=', ...)
.where('fieldE', '>=', ...)
.offset(random)
.limit(1)
.get()

Since I'm using up the <= and >= for fieldE, and since firestore allows inequalities on one field only, I'm trying to use the offset clause to get a random document.

My problem is: How do I assign a value to random such that I don't have an "out of bounds" condition? In other words, I don't want random to be greater than the number of documents that can ever be returned by the query...

Please help! This is a make-or-break issue for continuing with firestore in my current project, and I've come too far!

Note: I can't use this solution because of the fieldE problem.

kbrmys
  • 61
  • 6

0 Answers0