1

I have a structure like this

{
  comments: {
   ...anything here
  },
  flags: {
   flag1: true,
   flag2: false
  }
}

is there a way to query an exact object in firebase?

I've tried to use it like this.

const documents = await adminSdk.firestore()
   .collection('posts')
   .where('flags', '===', { flag1: true, flag2: false })
   .get();


console.log(documents.data());

but it returns nothing.

is there a way that I messed up or it's just out of scope for Firebase to do it like this?

Reyn
  • 757
  • 1
  • 5
  • 16
  • Possible duplicate of [Query based on multiple where clauses in Firebase](https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase) – vahdet Mar 06 '19 at 08:15
  • Not even close, as its for a multiple query more like an OR query. this one's more like an exact query of something – Reyn Mar 06 '19 at 08:26

2 Answers2

1

You can access to an object property just like in javascript with the dot.

.where('flags.flag1', '==', true)
.where('flags.flag2', '==', true)
MichelDelpech
  • 853
  • 8
  • 36
  • Thanks, but on the side note, how can you query a reference using where? @firebaser – Reyn Mar 06 '19 at 08:43
  • @Reyn have a look at this answer: https://stackoverflow.com/questions/53140913/querying-by-a-field-with-type-reference-in-firestore/53141199#53141199 – Renaud Tarnec Mar 06 '19 at 09:08
0

The operator you are using is not on the docs (firebase.google.com/docs/firestore/query-data/queries)

const documents = (await adminSdk.firestore()
   .collection('posts')
   .where('flags', '==', { flag1: true, flag2: false })
   .get()).docs.map(doc => doc.data());

should give you an array of posts