问题
I'm trying to store some certain information in the firebase realtime database. My database structure looks like this:
{
"contacts" : {
"-KdD1f0ecmVXHZ3H3abZ" : {
"email" : "ksdsd@sdsd.com",
"first_name" : "John",
"last_name" : "Smith",
"organization_id" : 1
},
"-KdG4iHEYjInv7ljBhgG" : {
"email" : "superttest@sds213123d.com",
"first_name" : "Max1",
"last_name" : "Rosse13131313l",
"organization_id" : 1
},
"-KdGAZ8Ws6weXWo0essF" : {
"email" : "superttest@sds213123d.com",
"first_name" : "Max1",
"last_name" : "Rosse13131313l",
"organization_id" : 1
}
}
}
I need to be able to specify condition like this SQL example:
SELECT * FROM contacts WHERE organization_id = :id LIMIT :start, :end
is there a way to do that in the database using Ruby or at least their REST API?
回答1:
The solution can be divided into two steps.
Step 1) You have to update rules in firebase Database if not already done.
{
"rules": {
"read": "auth !=null",
"write": "auth !null",
"REPLACE_WITH_ROOT_NODE_NAME": {
"contacts": {
"indexOn": ["organization_id"]
}
}
}
}
Step 2) To fetch data from firebase Database. The rest API request should be something like this
https://app-name.firebaseio.com/REPLACE_WITH_ROOT_NODE_NAME/contacts.json?auth=REPLACE_WITH_DATABASE_PASSWORD&orderBy="organization_id"&equalTo="REPLACE_WITH_ORGANIZATION_ID"&LimitToFirst=N
来源:https://stackoverflow.com/questions/42315302/specify-condition-and-limit-for-firebase-realtime-database-request