首页 > 解决方案 > firebase get child by month of birthday

问题描述

I'm looking to make query in firebase to get all child by month of birthday. for e.g the month is april (04) so I want to get the user1,user2,user4 in my example enter image description here

not sure if need to use in startAt query or what way to do it.

  let month = '04';
        firebase.ref('/UserTest').startAt(month).once('value',snapshot=>{
            snapshot.forEach((childSnapshot)=>{

            })
        })

标签: javascriptfirebasefirebase-realtime-database

解决方案


You need to store a field for month alone, then you can use equal(month_here) to be able to retrieve all users in month April:

  firebase.ref('/UserTest').orderByChild("month").equalTo(month).once('value',snapshot=>{
        snapshot.forEach((childSnapshot)=>{
        var users = childSnapshot.key;
        })
    })

This var users = childSnapshot.key; will retrieve the users

more info here:

https://firebase.google.com/docs/reference/js/firebase.database.Query


推荐阅读