1

My data is as follows:

const data = {
  papers: {
    id1: {
      name: 'test',
      annotated: {
        user1: true,
        user2: true,
      },
    },
  },
};

I want to return all papers that their annotated field does not contain the key "user1". How can I run such a query in a real-time database firebase? If it is not possible do you have any idea to restructure data in order to I can run such a query on this database?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ali Mirzaei
  • 1,496
  • 2
  • 16
  • 27
  • 1
    Have you checked [Firebase retrieve nodes with null field](https://stackoverflow.com/a/53927610/13130697)? – Dharmaraj Sep 22 '21 at 15:40
  • @Dharmaraj When I run orderByChild the key value should be indexed, but the problem is that the number of users are high and I do not have the users list. How can I index all of users? I get this error: Index not defined, add ".indexOn": "annotated/snPXLgfWEuOhbVbBkyABM7IAtTy1", for path "/papers", to the rules – Ali Mirzaei Sep 22 '21 at 15:49
  • 1
    Aside from the fact that you can't query on a value that isn't there, your structure also doesn't allow you to query for values in `annotated`. Simply put: your structure makes it easy to find the users for a specific paper. It does not however make it easy to find the papers for a specific user. For this you will need to add an inverted data structure that starts with the UIDs at the top level, and then adds paper IDs for each UID. See https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Sep 22 '21 at 16:43

0 Answers0