read only certain fields in firebase

前端 未结 2 1152
予麋鹿
予麋鹿 2020-12-11 10:00

If I have a database like

users: {
  user1: { 
    name:\'qwert\', 
    id: 123,
    num: 9999 
  },
  user2: { 
    name:\'zxcvb\', 
    id: 456,
    num:          


        
2条回答
  •  暖寄归人
    2020-12-11 10:45

    There is no way to select only the names from your current data model, Firebase always retrieves full nodes. So you will either have to do what Peter answered, reading all data but extracting the user name client side, or you will have to modify you data model to allow the use-case.

    The latter is quite simple. If you want to load a list of user names, you should store a list of user names in the database:

    users: {
      user1: { 
        name:'qwert', 
        id: 123,
        num: 9999 
      },
      user2: { 
        name:'zxcvb', 
        id: 456,
        num: 8888 
      }
    },
    usernames: {
      user1: 'qwert', 
      user2: 'zxcvb'
    }
    

    With this structure, it is trivial to read only the names.

    Also see:

    • Fetch particular fields from Firebase database
    • How to pull the data partially from firebase database
    • More efficient way to retrieve Firebase Data? (a more complex scenario of nesting data)

提交回复
热议问题