Skip to main content
added 3 characters in body; edited title
Source Link
marc_s
  • 759.9k
  • 186
  • 1.4k
  • 1.5k

How to query firebase realtime databsedatabase in cloud code

I am using firebaseFirebase cloud code and firebase realtime data database. My

My database structure is:

 -users
       -userid32
       -userid4734
          -flag=true  
       -userid722
          -flag=false
       -userid324

I want to query only the users who's field 'flag' is 'true' What i .

What I am doing currently is going over all the users and checking one by one. But this is not efficient, because we have a lot of users in the database and it takes more than 10 seconds for the function to run:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

exports.test1 = functions.https.onRequest((request, response) => {
    // Read Users from database
    //
    admin.database().ref('/users').once('value').then((snapshot) => {
        var values = snapshot.val(),
            current, 
            numOfRelevantUsers,
            res = {};       // Result string
            
            numOfRelevantUsers = 0;
            
        // Traverse through all users to check whether the user is elligibleeligible to get discount.
        for (val in values) 
        {
            current = values[val]; // Assign current user to avoid values[val] calls.
            
            // Do something with the user
        }
            
        ...
});

Is there a more efficient way to make this query and get only the relevant records? (and not getting all of them and checking one by one?)

How to query firebase realtime databse in cloud code

I am using firebase cloud code and firebase realtime data database. My database structure is:

 -users
       -userid32
       -userid4734
          -flag=true  
       -userid722
          -flag=false
       -userid324

I want to query only the users who's field 'flag' is 'true' What i am doing currently is going over all the users and checking one by one. But this is not efficient, because we have a lot of users in the database and it takes more than 10 seconds for the function to run:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

exports.test1 = functions.https.onRequest((request, response) => {
    // Read Users from database
    //
    admin.database().ref('/users').once('value').then((snapshot) => {
        var values = snapshot.val(),
            current, 
            numOfRelevantUsers,
            res = {};       // Result string
            
        numOfRelevantUsers = 0;
            
        // Traverse through all users to check whether the user is elligible to get discount.
        for (val in values) 
        {
            current = values[val]; // Assign current user to avoid values[val] calls.
            
            // Do something with the user
        }
            
        ...
});

Is there a more efficient way to make this query and get only the relevant records? (and not getting all of them and checking one by one?)

How to query firebase realtime database in cloud code

I am using Firebase cloud code and firebase realtime database.

My database structure is:

 -users
       -userid32
       -userid4734
          -flag=true  
       -userid722
          -flag=false
       -userid324

I want to query only the users who's field 'flag' is 'true' .

What I am doing currently is going over all the users and checking one by one. But this is not efficient, because we have a lot of users in the database and it takes more than 10 seconds for the function to run:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

exports.test1 = functions.https.onRequest((request, response) => {
    // Read Users from database
    //
    admin.database().ref('/users').once('value').then((snapshot) => {
        var values = snapshot.val(),
            current, 
            numOfRelevantUsers,
            res = {};       // Result string
            
            numOfRelevantUsers = 0;
            
        // Traverse through all users to check whether the user is eligible to get discount.
        for (val in values) 
        {
            current = values[val]; // Assign current user to avoid values[val] calls.
            
            // Do something with the user
        }
            
        ...
});

Is there a more efficient way to make this query and get only the relevant records? (and not getting all of them and checking one by one?)

edited tags
Link
Frank van Puffelen
  • 603.3k
  • 85
  • 895
  • 869
Source Link

How to query firebase realtime databse in cloud code

I am using firebase cloud code and firebase realtime data database. My database structure is:

 -users
       -userid32
       -userid4734
          -flag=true  
       -userid722
          -flag=false
       -userid324

I want to query only the users who's field 'flag' is 'true' What i am doing currently is going over all the users and checking one by one. But this is not efficient, because we have a lot of users in the database and it takes more than 10 seconds for the function to run:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

exports.test1 = functions.https.onRequest((request, response) => {
    // Read Users from database
    //
    admin.database().ref('/users').once('value').then((snapshot) => {
        var values = snapshot.val(),
            current, 
            numOfRelevantUsers,
            res = {};       // Result string
            
        numOfRelevantUsers = 0;
            
        // Traverse through all users to check whether the user is elligible to get discount.
        for (val in values) 
        {
            current = values[val]; // Assign current user to avoid values[val] calls.
            
            // Do something with the user
        }
            
        ...
});

Is there a more efficient way to make this query and get only the relevant records? (and not getting all of them and checking one by one?)