I'm able to get the data for a particular value in the child using orderByChild and equalTo (cool that it works for nested child as well)
private void getData() {
try {
final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database.child(Constants.TABLE_TASKS).orderByChild("user/id")
.equalTo("somevalue")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Timber.d(dataSnapshot.toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
Is there an easy way to get the data where a particular value is not found, basically something like a notEqualTo("somevalue") ?
In the Firebase Query model you can not filter for inequality to a value.
But you can test for the absence of any value (essentially: the absence of a property). For example with this data model:
{
child1: {
"user": {
"id": 1,
"name": "puf"
}
},
child2: {
"user": {
"id": 2,
"name": "abe"
}
},
child3: {
"user": {
"id": 3
}
}
}
I can query for children without a user/name property with:
ref.orderByChild('user/name').equalTo(null)
Which leads to the only child that doesn't have a name:
child3
Feel free to play with my jsbin to see if you get further: http://jsbin.com/liyibo/edit?js,console
Update: I knew I'd answered this before, but couldn't find it earlier. Here's the dupe: is it possible query data that are not equal to the specified condition?. It looks like I have a mistake in there, since clearly I'm testing for the absence of a property in the above code.
I think I've found the solution and this is more of how the database should be designed and actually now I understood the intention behind Firebase guideline
https://firebase.google.com/docs/database/android/structure-data
Original Design:
{
child1: {
"user": {
"id": "id1",
"name": "puf"
}
},
child2: {
"user": {
"id": "id2",
"name": "abe"
}
},
child3: {
"user": {
"id": "id1"
"name": "puf"
}
}
}
Updated Design:
So apart from the storing the id and name of the user, we should also store a node with the id itself as the key and mark it to true
{
child1: {
"user": {
"id": "id1",
"name": "puf"
"id1": true
}
},
child2: {
"user": {
"id": "id2",
"name": "abe"
"id2": true
}
},
child3: {
"user": {
"id": "id1"
"name": "puf"
"id1": true
}
}
}
With the updated design, if i execute ref.orderByChild('user/id1').equalTo(true)
I would get output as Child1 and Child 3
and if i execute ref.orderByChild('user/id1').equalTo(null),
I would get Child2 as the output
Related
I have a class which is part of a school and this class has teachers and students, all of them has name and maybe has phone number , I want to get the full data for the classes
but firstly, could you advice me, what is the best for performance and maintaining from the following Dbs :
1st one
"schools":{
"school1":{
"class1":{
"name":"SC1",
"teachers":[{
"name":"T1"
}, {
"name":"T2"
}],
"students":[
{"name":"S1"},
{"name":"S2"}
]
}
}
.
.
.
.
.
.
.
}
and the 2nd
"school":{
"school1":{
"name":"SC1"
},
"school2":{
"name":"SC2"
}
},
"classes": {
"class1": {
"name": "C1"
},
"class2": {
"name": "C2"
}
},
"students": {
"student1": {
"name": "S1",
"phone":"123456789"
},
"student2": {
"name": "S2",
"phone":"123456789"
},
"student3": {
"name": "S3",
"phone":"123456789"
},
"student4": {
"name": "S4",
"phone":"123456789"
}
},
"teachers": {
"student1": {
"name": "T1",
"phone":"123456789"
},
"student2": {
"name": "T2",
"phone":"123456789"
},
"student3": {
"name": "T3",
"phone":"123456789"
},
"student4": {
"name": "T4",
"phone":"123456789"
}
},
"classes_enrollments": {
"class1": {
"teacher1": true,
"teacher3": true,
"student1": true,
"student2": true
},
"class2": {
"teacher2": true,
"teacher4": true,
"student3": true,
"student4": true
},
"class3": {
"teacher1": true,
"teacher2": true,
"student3": true,
"student4": true,
"student1": true,
"student2": true
}
},
"student_friends": {
"student1": {
"student2": true
},
"students2": {
"student1": true,
"student3": true
},
"students3": {
"student2": true
}
},
"teacher_friends": {
"teacher1": {
"teacher2": true
},
"teacher2": {
"teacher1": true,
"teacher3": true
},
"teacher3": {
"teacher2": true
}
}
and for the 2nd way how to get the full data for the class1: in which school and it's name and count of teachers and students and their names and phones
Thank you
I would mix those two.
For code simplicity and reading performance of individual class details, the 2nd scheme would indeed be messy. The 1st scheme would be better, but with some improvements.
Keep the teachers and students paths at root, just like in the 2nd scheme.
Add teacher_enrollments and student_enrollments path at root, to save the ids of the classes that each teacher/student is associated with.
Don't save class teachers and students as arrays inside classes, but use maps instead, similar to what you're saving in the root teachers and students path.
That way, when you edit a teacher from the root path, you can also get a list of all their associated classes (the ids) from the enrollments path, and do a multi-path update for these classes, to update the teacher/student details in each associated class.
If you have lots of data, you might want to maintain a separate path for class summaries, so that you can easily show a list of classes, without having to download the data for all included teachers and students (which would be present multiple times in all these classes).
When you delete a class, you would also want to do a multi-path update to delete all associated enrollments. If the total number of students and teachers is not too big, you can just delete the enrollments for ALL teacheres/students. If you have lots of teachers/students, you could keep your classes_enrollments path (but with intermediate teachers and students before the ids), so that you can make an update with only the required teacher/student ids. (it's actually a lot simpler. You already have the teacher/student IDs in the class info)
// How to delete a class in JavaScript.
// For Java, use updateChildren() instead of update(),
// and supply it with a HashMap instead of a plain object.
const classToDelete = { id: 'class1', teachers: ..., students: ..., school: ... };
const updateObject = {
['classes/'+classToDelete.id]: null },
['schools/'+classToDelete.school.id+'/classes/'+classToDelete.id]: null },
};
Object.keys(classToDelete.teachers).forEach(teacherId => {
updateObject['teachers/'+teacherId +'/classes/'+classToDelete.id] = null;
});
Object.keys(classToDelete.students).forEach(studentId=> {
updateObject['students/'+studentId+'/classes/'+classToDelete.id] = null;
});
dbRef.update(updateObject);
Example database structure (slightly different than instructed, but using the same concepts):
"schools": {
"school1": {
"id": "school1",
"name": "The best school",
"classes": {
"class1": {
"id": "class1",
"name": "The best class"
}
}
}
},
"classes": {
"class1": {
"id": "class1",
"name": "The best class",
"teachers": {
"teacher1": {
"id": "teacher1",
"name": "The best teacher",
"phone": "123"
}
},
"students": {
"student1": {
"id": "student1",
"name": "The best student",
"phone": "456"
}
},
"school": {
"id": "school1",
"name": "The best school"
}
}
},
"teachers": {
"teacher1": {
"id": "teacher1",
"name": "The best teacher",
"phone": "123",
"classes": {
"class1": {
"name": "The best class",
"school": {
"id": "school1",
"name": "The best school"
}
}
}
}
},
"students": {
"student1": {
"id": "student1",
"name": "The best student",
"phone": "456",
"classes": {
"class1": {
"name": "The best class",
"school": {
"id": "school1",
"name": "The best school"
}
}
}
}
}
Good luck!
I want to sort my main array using a child array value. Output as below:
{
"status": true,
"statusCode": 100,
"message": "",
"data": {
"win": [
{
"id": "1",
"admin": [
{
"id": "38",
"name": "Admin 05",
"point": {
"id": "96",
"name": "96"
}
}
]
},
{
"id": "2",
"admin": [
{
"id": "39",
"name": "Admin 06",
"point": {
"id": "95",
"name": "95"
}
}
]
},
{
"id": "3",
"admin": [
{
"id": "26",
"name": "Admin 05",
"point": {
"id": "98",
"name": "98"
}
}
]
}
]
}
}
I want array as below but I am not able to do so.
id = 3 // first
id = 1 // second
id = 2 // third and so on.
I tried below:
Collections.sort(loWinArray, object : Comparator<Win> {
override fun compare(obj1: Win, obj2: Win): Int {
// ## Ascending order
return (obj2.admin.get(0).point!!.name.toInt() - obj1.admin.get(0).point!!.name.toInt()) // To compare string values
}
})
Can any one please help?
If you want it to be sorted by the point ID, largest first to smallest last, try this:
val sortedArray = loWinArray.sortedByDescending { item -> item.admin.first().id.toInt() }
Try this for ascending order what you have tried it will return descending order
Collections.sort(loWinArray, object : Comparator<Win> {
override fun compare(obj1: Win, obj2: Win): Int {
// ## Ascending order
return obj1.admin.get(0).point!!.name.toInt() - obj2.admin.get(0).point!!.name.toInt()
}
})
I am creating an Android App.The App have a feature that retrieve Facebook posts using Graph API.
My Problem
My Problem is , the received JSON lacks the following fields:
Url for the images attached with the comments
Replies to the comments
Only No. of likes available. No Reactions list
For posts containing more than one images , full_picture fields contains link to only one image. Where is the link for other images ?
I used the /me/posts to get the posts.
Why I can't get the above details ?
Steps taken
I searched Stackoverflow but The answers only mentioned how to get images and replies for individual commment(by using comment id) , but no reference how to get it alongs with the JSON received from "/me/posts". (So , this is not a duplicate!!!!)
My Queries
Why I can't get the above details ?
What is the way to get comment's Images , replies,url for all images of the Parent post and Posts reactions in bundle.(that is along with the JSON of "/me/posts" without running /comment request for each and every comment?
My Code
Code the executes Graph Query
login_button.setReadPermissions(Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends","user_posts", "user_status")); // Setting permissions
Bundle params = new Bundle();
params.putString("fields", "message,created_time,id,full_picture,status_type,source,comments.summary(true),likes.summary(true)");
params.putString("limit", "100");
new GraphRequest( loginResult.getAccessToken(), "/me/posts", params, HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
try {
EditText postsText = (EditText) findViewById(R.id.postText);
String res = response.toString();
res = res.replaceAll("\\{Response:\\s*\\w*\\W*\\s*[0-9]*\\W*\\w*:", "");
res = res.replaceAll("\\,\\s*error:\\s*null\\W*\\s*","");
postsText.setText(res);
writeToFile(res,"response");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
).executeAsync();
}
#Override
public void onCancel() {
txtstatus.setText("Login canceled");
}
#Override
public void onError(FacebookException error) {
txtstatus.setText("Login failed"+error.getMessage());
}
});
JSON
{
"data": [
{
"message": "'A reader lives a thousand lives before he dies. The man who never reads lives only one.'\n- George RR Martin",
"created_time": "2018-01-28T12:17:54+0000",
"id": "113326729482474_113029916178822",
"full_picture": "https://scontent.xx.fbcdn.net/v/t1.0-9/26993527_113029839512163_7617357733573673432_n.jpg?oh=c617cf94e3cdd62320fde60e445f760e&oe=5B22BC72",
"status_type": "added_photos",
"likes": {
"data": [],
"summary": {
"total_count": 0,
"can_like": true,
"has_liked": true
}
},
"comments": {
"data": [
{
"created_time": "2018-01-28T12:23:04+0000",
"from": {
"name": "Ragesh D Antony",
"id": "1845099915531898"
},
"message": "Such a wonderful Quote ..",
"id": "113029916178822_113036856178128"
},
{
"created_time": "2018-01-28T13:59:22+0000",
"from": {
"name": "Vignesh Lakshmanen",
"id": "1558536637578045"
},
"message": "Superb",
"id": "113029916178822_113251916156622"
}
],
"paging": {
"cursors": {
"before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEV6TURNMk9EVTJNVGM0TVRJNE9qRTFNVGN4TkRJeE9EUT0ZD",
"after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEV6TWpVeE9URTJNVFUyTmpJeU9qRTFNVGN4TkRjNU5qST0ZD"
}
},
"summary": {
"order": "chronological",
"total_count": 2,
"can_comment": true
}
}
},
{
"message": "\"It's an insane world, but in it there's one sanity, the loyalty of old friends. Friends, we must believe in one another.\" \n\n- Ben Hur",
"created_time": "2018-01-28T12:14:27+0000",
"id": "113326729482474_113026646179149",
"status_type": "mobile_status_update",
"likes": {
"data": [
{
"id": "1845099915531898",
"name": "Ragesh D Antony"
},
{
"id": "1558536637578045",
"name": "Vignesh Lakshmanen"
}
],
"paging": {
"cursors": {
"before": "MTg0NTA5OTkxNTUzMTg5OAZDZD",
"after": "MTU1ODUzNjYzNzU3ODA0NQZDZD"
}
},
"summary": {
"total_count": 2,
"can_like": true,
"has_liked": false
}
},
"comments": {
"data": [
{
"created_time": "2018-01-28T12:24:40+0000",
"from": {
"name": "Ragesh D Antony",
"id": "1845099915531898"
},
"message": "I like Ben Hur very much",
"id": "113026646179149_113040629511084"
}
],
"paging": {
"cursors": {
"before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEV6TURRd05qSTVOVEV4TURnME9qRTFNVGN4TkRJeU9EQT0ZD",
"after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEV6TURRd05qSTVOVEV4TURnME9qRTFNVGN4TkRJeU9EQT0ZD"
}
},
"summary": {
"order": "chronological",
"total_count": 1,
"can_comment": true
}
}
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.11/113326729482474/posts?fields=message,created_time,id,full_picture,status_type,source,comments.summary%28true%29,likes.summary%28true%29&limit=100&format=json&since=1517141874&access_token=EAATh9tg5DMIBANl59BIPExJRzwZAhedH6PSsV2ZAO9FvzFiGqAW3HKafH9b7Bb0gSKnQMJktzUhX2DZCB29BezRUfI2HOumZAYp6FFYwOBJA1ZCdaE4ZCNLXB1ctUZBaKwbp8lXlZBwZBLLCG25CnGAckZClJXZA5omRDFtVvnJFTkJpqigUmy22QNGFmF4CgfC6sWEuVin7mAI9WZCkKkNVzSEeDGDoitt7xeVFiHXCetKOVoLZAl3qyfrYj&__paging_token=enc_AdC03uweQZCRZBN6tZCzhAQ50BZCKzx5koYtAZBRUmgY94TKcCrj6owvzXw9lpoL4iBKJUkW1ZAz4MoZAZBBaGSLZAHTHsKuEst7gtKaQrsQoiZBx8sO2QjQZDZD&__previous=1",
"next": "https://graph.facebook.com/v2.11/113326729482474/posts?fields=message,created_time,id,full_picture,status_type,source,comments.summary%28true%29,likes.summary%28true%29&limit=100&format=json&access_token=EAATh9tg5DMIBANl59BIPExJRzwZAhedH6PSsV2ZAO9FvzFiGqAW3HKafH9b7Bb0gSKnQMJktzUhX2DZCB29BezRUfI2HOumZAYp6FFYwOBJA1ZCdaE4ZCNLXB1ctUZBaKwbp8lXlZBwZBLLCG25CnGAckZClJXZA5omRDFtVvnJFTkJpqigUmy22QNGFmF4CgfC6sWEuVin7mAI9WZCkKkNVzSEeDGDoitt7xeVFiHXCetKOVoLZAl3qyfrYj&until=1517140875&__paging_token=enc_AdC1r6ZBwBybSmO16DuZCEWGDbdqTeiKCbTsa7h6WDenaZBgB40Xbc0flGcwYqiYPFiEf9wEvuBSoEvbfyl25J0JlbyfhKA1ZBufZC5ZAJMqLMg6bAXgZDZD"
}
}
Well, regarding "reaction list", this is available only for the Page owner (starting from 2018 Febraury).
Read the Graph API docs:
https://developers.facebook.com/docs/graph-api/reference/v2.12/post
"For Posts on a Page:
Any valid access token can read posts on a public Page, but responses will not include User information.
A Page access token can read all Posts posted to or posted by that Page, and responses will include User information."
I am attempting to query some hierarchical data in firebase. I'm having a little difficulty in figuring out how to query the following data structure:
{
"orgs": {
"-KBFXBBEyvgtfqMvU4pi": {
"name": "ACME 123",
"owner": "-K9IPqIUIuEFzLS0f_Pe",
"users": {
"-KBF_GhwTmXfR6Jce30t": {
"email": "userA#company.com",
"permission": "editor",
"userKey": "K99LV9cTjh1ovW1D5j2"
},
"-KBF_M533zzbUilGvAAW": {
"email": "userB#company.com",
"permission": "editor"
}
}
},
"-KBFaKlJ8tfqjBQjAZgq": {
"name": "ACME Alt LLC",
"owner": "-K9IPqIUIuEFzLS0f_ZZ",
"users": {
"-KBFbD4trt9nyeHPUQbn": {
"email": "userX#co.com",
"permission": "editor"
}
}
}
}
}
Specifically, I would like to find out if the email address "userB#company.com" exists. But this is a little confusing for me since I need to search through 2 levels (orgs and users).
After reading some more documentation it seems I really shouldn't be nesting my data like this. I'll be honest, it seems kind of contrary to do this when working with a JSON schema that that is hierarchical. Anyway, this is what I'm looking to do now:
{
"orgs": {
"-KBFXBBEyvgtfqMvU4pi": {
"name": "ACME 123",
"owner": "-K9IPqIUIuEFzLS0f_Pe"
}
},
"-KBFaKlJ8tfqjBQjAZgq": {
"name": "ACME Alt LLC",
"owner": "-K9IPqIUIuEFzLS0f_ZZ"
}
}
},
"orgMembership": {
"-KBFXBBEyvgtfq7h381h": {
"org": "-KBFXBBEyvgtfqMvU4pi",
"email": "userA#company.com",
"permission": "editor"
}
}
Then I can use the following query:
orgMRef.orderByChild("email").equalTo("userA#company.com").once("child_added", function(snapshot) {
console.log("found: " + snapshot.key());
});
I'm developing an Android app with the following Firebase database table:
"posts": {
"id_1": {
"author": "google:111527135678918251124",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 0,
"visible": true
},
"id_2": {
"author": "google:111527135678918251524",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 2,
"visible": true
},
"id_3": {
"author": "google:111527135678918251124",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 1,
"visible": true
}
}
I'd like to be able to retrieve a the posts sorted by a child (sorted by type as an example) and be able to retrieve all the posts with a specific child value (type = 1).
Reading the Firebase docs it seems I have to write the following code, but I don't get the wanted result.
Order by type
rootRef.child("posts").orderByChild("type")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
result = (HashMap<String, Post>) dataSnapshot.getValue();
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
I get: an unordered Map of Posts
Type = value
rootRef.child("posts").orderByChild("type").equalTo(1, "type").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
result = dataSnapshot.getValue();
result = result;
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
})
I get: null.
My Datasnapshot object has key = "posts" and value = null
About the ordering, I think the problem is that I'm retrieving data as a HashMap which is itself unordered.
As suggested in the comments the second case needs .equalTo(1) instead of .equalTo("type",1) because it's preceded by the .orderBy() method.