0

I want to get random data from collection expect the given value.

async function selectCompany(companyName) {
   const vouchercompany = [];
try{
    await db.collection('companies')
    .where ('companyName' ,'!=' , companyName)
    .get()
    .then((querySnapshot) => {
      querySnapshot.forEach((doc) => {
   vouchercompany.push(doc.data())
      })
    })
    return vouchercompany;
} catch(error) { json(error.message) }

Any suggestion would be helpful

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Irfana
  • 21
  • 1
  • 5

1 Answers1

0

Getting random documents from a collection is pretty definitively described here: Firestore: How to get random documents in a collection

But since the query limitations in the Firestore documentation say:

In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.

That means you can't use one field for a random index, and then still filter for != on companyName. You'll either have to store both values in the same field, or do the filtering for companyName in your application code after retrieving the random documents.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807