Using CBLite Android, and storing docs (similar to Android Grocery Sync example) like this:
{
"check" : true,
"created_at" : "2014-02-25T10:16:46.026Z",
"text" : "Soap",
"prices":[ { "date" : "2014-02-25" , "value" : 12 } ]
}
I get value in "text" field as shown in the documentation:
...
Document document = row.getDocument();
String text = ((String)document.getCurrentRevision().getProperty("text"));
...
But, How should I do to get the value in, for example, "value" field?
Thanks.
Edit:
Getting documents
API reference
Search in the doc to something which allow you to get an array, then read the array to get value.
Related
The whereEqualTo API can take Any as its argument. Does this mean that I can pass it a HashMap<K, V> and it will compare its matching fields with that of a field of map type in the Firestore database or do I have to manually compare each property of the map?
I've build a query like this one
collection.whereEqualTo("meta", hashMapOf( // <-- using full hash-map for matching
"user" to "JD",
"name" to "John"
))
where meta is a map-type field with two key/value pairs.
The document I'm trying to match has this structure:
{
"createdOn": as timestamp
...other properties
"meta": as map
{
"user": "JD", as string
"name": "John" as string
}
}
The criteria I use should yield some results but it doesn't (without crashing) so I was wondering whether I made a mistake somewhere else or is this type of a query simply not supported? The result is empty.
I might also be searching only for the name property with a simpler query like this one to find all Johns
collection.whereEqualTo("meta", hashMapOf( // <-- using parcial hash-map for matching
"name" to "John"
))
Will it match only name fields or does it require a full-map?
I mean, should the previous query work or do I need to rewrite as this?
collection
.whereEqualTo("meta.user", "JD")
.whereEqualto("meta.name", "John")
Firestore can compare items of a map in your client code with a map in a document. But the maps must be completely equivalent, so you must specify all properties in your code. So something like this should work:
collection.whereEqualTo("meta", hashMapOf( // <-- using parcial hash-map for matching
"user" to "JD",
"name" to "John"
))
If you can't get this to work, edit your question to include a screenshot of the document you think this should match and I'll have another look.
There is no way specify partial map matches for a field, so if that's what you need, you'll indeed need to add separate conditions for each nested field.
Suppose I have a Firebase database in my Android App and structured as below:
{"firebase_db" : {
"users" : {
"user_uid_0" : {
"name" : "name_0",
"score" : 2000
},
"user_uid_1" : {
"name" : "name_1",
"score" : 3000
},
"user_uid_2" : {
"name" : "name_2",
"score" : 1000
},
"user_uid_3" : {
"name" : "name_3",
"score" : 4000
},
"user_uid_4" : {
"name" : "name_4",
"score" : 0
}
}
}
}
and I would like to sort them by the child "score". However, I do not want an entire list as the return (which can be easily achieved using the orderByChild() and limitToLast() methods). Instead, I want to know at what place a specific node is when ranked by the child "score" in descending order. For example, if I give the input "user_uid_0", then it should return 3 (the 3rd place); if the input is "user_uid_2", then it should return 4 (the 4th place)
But, I could not find a way to achieve this in several lines of code. Until now, the only way I've come up with to solve this problem efficiently was write a cloud function on Firebase... Is there any simpler way to do this?
Yes, you can solve this on user side. The simplest way I can think of is to query the database according to your needs to get all user objects. Add all this objects to a map, in which the key of the map is the user id and the value is the position. In the end, just iterate over the map and get the position according to your user id. That's it!
I'm building an Android application in which the user can track its weight and progress by inserting their weight (optional) AND/OR upload a picture (optional).
{
"users" : {
"5aHvYy5wTIVAAgdOEso0wr2woHS2" : {
"firstName" : "Justin",
"logs" : [ {
"date" : "16/05/2018",
"progressPicture" : "http://www.google.com/example.jpg",
"weight" : 82.1
}, {
"date" : "17/05/2018",
"progressPicture" : "http://www.google.com/example.jpg"
}, {
"date" : "18/05/2018",
"weight" : 80.3
} ]
}
}
I have managed to get the latest log by ordering by key and limiting the result to 1, using the following code.
firebaseDatabase.getReference(USER_REFERENCE).child(firebaseAuth.currentUser?.uid).child(LOG_REFERENCE).orderByKey().limitToLast(1).
As can be seen from my JSON, a log doesn't necessarily have to have a weight value.
MY QUESTION:
How can I adjust my query so that I'll retrieve my latest log (so 1 log only based on key) where there is a value for weight?
You can add new entry called weight_log,which will be updated once new weight is recorded , you can do it easily with firebase functions that will be triggered on each
/users/{userId}/weight_log/{iterationID} create event
Then you can use limitToLast(1) on the new path
Generally it is good practice to flattening your data structure
From reading the wiki you can only filter the data and check if there's a value for weight by adding a ValueEventListener() which will return a list of all the list where you have to loop through it and get you value
https://firebase.google.com/docs/database/android/lists-of-data#filtering_data
"news" : {
"-KnLRSIOyD7HgldFGty" : {
"caption" : "some grounds",
"content" : "some contents",
"created_on" : 1498246403444,
"newspaper_id" : "-KnLPu2N5039ZbqS",
"status" : false,
"thumbnail" : "thumbnail/1498254125498",
},
}
"newspapers" : {
"-KnLRSIOyD7HgldFGty" : {
"logo" : "logo/1498245996906",
"paper_name" : "NewsOnline"
},
}
I want to filter firebase to do something like this.
mDatabaseRef.child("news").orderByChild("newspaper_id").equalTo(id).orderByChild("status").equalTo(false);
Since multiple orderBy() will throw exception in firebase, what is the other way to achieve the filter.
Currently Firebase supports only a single orderBy per query.
So if you want to filter or order the results on more than one property (2 query in your case), you'll either have to do the additional filtering (or ordering) client-side in your Android code or you'll have to come up with your own indexing scheme that combines the properties.
In your case i suggest you add a new child named newspaperId_status which can get the value as concatenation of both values like this:
newspaperId_status: -KnLPu2N5039ZbqS_false
In this way you can query all newspappers which have the status of false.
Hope it helps.
I am using flashlight which provides a solution to make content search over firebase database and i am facing a problem that I have a database consists of posts where any post consists of:-
{
title : "",
Body :""
TimeStamp :""
Author :""
}
and in the config.js, I defined my paths as follow
exports.paths = [
{
path : "posts",
index: "firebase",
type : "thing",
fields: ['title']
}}
the result of the search contains:-
_source {
title : ""
}
How do i make the ES to return all the post document fields not only the "title" field?
fields:['title'] means that title is the only field indexed. The fields: parameter says "index these properties and these properties only" Basically, you are indexing only the title property, so there's nothing else to be returned when querying type thing.
To return your whole post object, simply remove the fields: property when defining your path. This will default to index everything in your https://<instance>.firebaseio.com/posts path.