I'm trying to get some data of Firebase. I'm tring to do orderby().equalTo() but I'm always getting null in my query. I've tried with two values deliveredBy (boolean) and deliveredBy (long).
Query
queryFirebase = FirebaseDatabase.getInstance().getReference().child("rooms").orderByChild("read").equalTo(false);
queryFirebase.addListenerForSingleValueEvent(new ValueEventListener() {
Send my message to Firebase
Long userId = ApplicationConfig.getCurrentUser().getId();
String toServerUnicodeEncoded = StringEscapeUtils.escapeJava(etMessage.getText().toString());
MessageFirebase message = new MessageFirebase(toServerUnicodeEncoded, new DateTime().toString("yyyy-MM-dd'T'HH:mm:ss'Z'"), userId);
DatabaseReference messageRef = mFirebaseDatabaseReference.push();
messageRef.setValue(message);
MY JSON
{
"rooms" : {
"-KtMBWKEf7-Ot8HdMtbd" : {
"-KtMC7CglZZ-Yp4_PQOU" : {
"blocked" : false,
"createDate" : "2017-09-06T14:40:17Z",
"delivered" : false,
"deliveredBy" : 317,
"difDays" : 0,
"isBlocked" : false,
"message" : "hola",
"read" : false
},
"-KtMCCdbKzP7UKFTB_qG" : {
"blocked" : false,
"createDate" : "2017-09-06T14:40:39Z",
"delivered" : false,
"deliveredBy" : 317,
"difDays" : 0,
"isBlocked" : false,
"message" : "que tal",
"read" : false
},
"commerceId" : 218,
"userId" : 317
}
}
}
read is not a direct child of rooms
You should do
queryFirebase = FirebaseDatabase.getInstance().getReference()
.child("rooms")
.child("-KtMBWKEf7-Ot8HdMtbd")
.orderByChild("read")
.equalTo(false);
queryFirebase.addListenerForSingleValueEvent(new ValueEventListener() {
Related
Need to add a new node "Completed" under the parent which will eventually contain all the information from the node "Requests" at end of the ride.
So basically, I need to create node "Completed" first but its not adding it.
database
{
"Requests" : {
"iowpxU6WKUWpzWJyfssSoOVCPFj2" : {
".priority" : "f8118c3k3v",
"destination" : "221 Prince William St",
"details" : {
"driver" : "nYIAHSYimJMHbMkXqDt9PQ0U3Nf2",
"location" : "27 Horsfield St",
"request status" : "accepted",
"ridePrice" : 3.75,
"rideStatus1" : "arrived at pickup",
"rideStatus2" : "rider in vehicle",
"rideStatus3" : "destination bound",
"rideStatus4" : "arrived at destination",
"rider" : "iowpxU6WKUWpzWJyfssSoOVCPFj2",
"riderPaid" : "true"
},
"Users" : {
"Drivers" : {
"nYIAHSYimJMHbMkXqDt9PQ0U3Nf2" : {
"driver" : "nYIAHSYimJMHbMkXqDt9PQ0U3Nf2",
"email" : "driver#me.com",
"name" : "driver",
"password" : "whatever",
"phone" : "5551212",
"rates" : "0"
}
},
"Riders" : {
"iowpxU6WKUWpzWJyfssSoOVCPFj2" : {
"avatarUrl" : "",
"email" : "rider#me.com",
"name" : "rider",
"password" : "whatever",
"phone" : "5551313",
"rates" : "0",
"riderId" : "iowpxU6WKUWpzWJyfssSoOVCPFj2"
}
}
}
}
addNewNode
private void addNewNode() { // TODO: ........ NOT ADDING NEW NODE :-| ..........
Toast.makeText(this, "addNewNode", Toast.LENGTH_LONG).show();
DatabaseReference newNode = FirebaseDatabase.getInstance().getReference("Completed");
newNode.child(riderId).push().addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(DriverTripDetail.this, "addNewNode: onDataChange", Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Any assistance on how I can fix this would be greatly appreciated.
The Firebase Database stores values at paths. If there is no value, the path does not exist.
Your code creates a reference to /Completed/$pushID. but doesn't set any value, so the path doesn't get created. Something like this will work, since it sets a value:
newNode.child(riderId).push().setValue(true)
Let's consider a "UserBean" with a property "emails" that is an ArrayList<EmailBean>.
When saved in Firebase it's like :
"users" : {
"$userID" : {
"emails" : [
"0" : {
"type" : "first",
"address" : "google#google.com",
"private" : true
},
"1" : {
"type" : "second",
"address" : "firebase#firebase.com",
"private" : false"
}
],
"name" : "Mitch"
}
}
So is it possible to achieve something like this :
"users" : {
"$userID" : {
"emails" : [
"first" : {
"address" : "google#google.com",
"private" : true
},
"second" : {
"address" : "firebase#firebase.com",
"private" : false
}
],
"name" : "Mitch"
}
}
I would prefer to not having to save each address one at a time.
Maybe there's an annotation to tells Firebase to use this property as a key ?
Regards.
I recomend you using the code below:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference emailsRef = rootRef.child("users").child(userId).child("emails");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String type = ds.child("type").getValue(String.class);
ds.child("type").getRef().getParent().setValue(type);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
emailsRef.addListenerForSingleValueEvent(eventListener);
What have i tried with this code? So in order to achieve what you want, first of all we got the value of your type key. Then we get a step up in the tree and set the partent with the value of your type key for each particular node.
Having the value of type in stead of 0, 1 and so on, to get the work done, just remove the type key, from each node using removeValue() method directly on the reference.
I cannot update one field on firebase, when i do delete all the others fields of the database
CODE
String URL_EVENT_REFERENCE = "eventmodel";
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference databaseEvent = database.getReference(URL_EVENT_REFERENCE);
String key = "KTVAt0NM1Y5v32YxSqc";
Map<String, Object> eventValues = new HashMap<>();
eventValues.put("name", "new name");
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put(key, eventValues);
databaseEvent.updateChildren(childUpdates);
This is the JSON tree, on the eventmodel y want to update the -KTVAt0NM1Y5v32YxSqc and change the name hola to new name
{
"eventmodel" : {
"-KTVAt0NM1Y5v32YxSqc" : {
"info" : "",
"limit" : 2,
"members" : {
"Mn7gZ7rn5XRGU1HXujbbSQv05BH2" : true
},
"name" : "hola",
"pic" : "default_publish_img",
"place" : {
"address" : "Barcelona, EspaƱa",
"latitude" : 41.3850639,
"longitude" : 2.1734034999999494
},
"status" : true,
"timeCurrentEnd" : "7/10/2016 21:27",
"timeCurrentStart" : "7/10/2016 20:27",
"uidLeader" : "Mn7gZ7rn5XRGU1HXujbbSQv05BH2"
}
}
}
You are putting new empty object with only name field..
try to update only the name instead of the whole object
databaseEvent.child(key).child("name").setValue("new name");
I'm trying to retrieve all recipes favorited by a user using this code, but no data is retrived
recipesQuery = mDatabase.child("recipes").orderByChild("favoritedBy").equalTo(getId());
This is my Structure:
{
"-KNnrgEmF8qCpsWiIMW4" : {
"favoriteCount" : 4,
"favoritedBy" : {
"0ZtxMXZ8iDV4u0POxu8n6cIwCX33" : true
},
"name" : "Recipe 1",
"recycleCount" : 0,
},
"-KNvq6Ts8RYxc0-O3B2t" : {
"favoriteCount" : 2,
"favoritedBy" : {
"0ZtxMXZ8iDV4u0POxu8n6cIwCX33" : true
},
"name" : "Recipe 2",
"recycleCount" : 0,
}
}
If I remove the .equalTo() method all data is retrieved, but thats not what I want.
What am I doing wrong?
There's currently no method to filter a query based on other objects than String, Boolean, or Double.
What I suggest is to restructure your database to something like this
{
"users": {
"userId1" : {
"name" : "User1",
"favorites": {
"-KNnrgEmF8qCpsWiIMW4" : true,
"-KNvq6Ts8RYxc0-O3B2t" : true
}
},
"userId2" : {
"name" : "User2",
"favorites": {
"-KNvq6Ts8RYxc0-O3B2t" : true
}
}
},
"recipes": {
"-KNnrgEmF8qCpsWiIMW4" : {
"favoriteCount" : 4,
"name" : "Recipe 1",
"recycleCount" : 0,
},
"-KNvq6Ts8RYxc0-O3B2t" : {
"favoriteCount" : 2,
"name" : "Recipe 2",
"recycleCount" : 0,
}
}
}
Then it's easier to get the recipes favorited by specific user.
mDatabase.child("users").child(userid).child("favorites")
I wish to retrieve only the top 50 messages a user has within the database, with schema:
"individualMessages" : {
"99e4989b-a046-4c5f-9478-5ebd8bdc3ded" : {
"a96da7b1-7c4e-44bc-b82e-fc75bed52bcd" : {
"-KBUjngi9JStE-k4RMUJ" : {
"from" : "a96da7b1-7c4e-44bc-b82e-fc75bed52bcd",
"messageText" : "hey",
"timeSent" : "2016-02-26 17:40:46"
},
"-KBYhsRONfGxVI7Ysb3F" : {
"from" : "a96da7b1-7c4e-44bc-b82e-fc75bed52bcd",
"messageText" : "pool",
"timeSent" : "2016-02-27 12:10:52"
}
},
"fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de" : {
"-KBcYtQ5gD-drFZqz_MU" : {
"from" : "fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de",
"messageText" : "ouu",
"timeSent" : "2016-02-28 10:46:52"
},
"-KBcYw24M-9-7H5A3czD" : {
"from" : "fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de",
"messageText" : "gsf",
"timeSent" : "2016-02-28 10:47:03"
}
However, simply using:
individualMessageMyPath = myFeastFirebase.child("individualMessages").child(.....
getTop50Messages = individualMessageMyPath.limitToLast(5);
getTop50Messages.orderByChild("timeSent").addChildEventListener(mainMessagingListener);
Returns all messages on that node.
Reviewing the documentation this should work, however it does not.
What is missing?