Android: parse.com query - android

I am using Parse as database.
I would like to ask if there are over 1000 items in the Data_db, where the username is unique.
Code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Data_db");
query.whereEqualTo("username", edittext_user.getText().toString());
query.setLimit(1);
Question:
While I know fetching over 1000 dataset we need to use the .setSkip(1000) function and performing looping, I would like to ask how about querying a specific user? Is it also limited to the first 1000 rows? and if yes, how could such user beyond 1000 could be queried?
Thanks!

You're going about this the wrong way, your query will be inefficient as you're looking 1 object when those constraints through an entire class, regardless of it being already found. It would be better to use getFirstInBackground(). Something like:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Data_db");
query.whereEqualTo("username", edittext_user.getText().toString());
query.getFirstInBackground(...);
A maximum of 1000 objects can be returned to you through a single query, by using getFirstInBackground(), you're searching though the database, hence that limitation is not applied.

Related

Fetching items from dynamoDB with sorting

In my project i want to fetch all records from table with sort by created_date descending order.
Also i want to add condition of fetch all item not created_by login user.
I have tried many ways but not able to achieve it.
Below is my table structure.
Here is my java code to fetch records from dynamoDB.
Map<String, Condition> filter = new HashMap<String, Condition>();
//filter.put(RealmConstant.Expo.created_by, new Condition().withComparisonOperator(ComparisonOperator.NE).withAttributeValueList(new AttributeValue().withS(userId)));
filter.put(RealmConstant.Expo.created_date, new Condition().withComparisonOperator(ComparisonOperator.LE.toString()).withAttributeValueList(new AttributeValue().withS(""+new Date())));
Expo expo =new Expo();
expo.setCreated_by(userId);
DynamoDBQueryExpression<Expo> queryExpression = new DynamoDBQueryExpression<Expo>();
queryExpression.setHashKeyValues(expo);
queryExpression.setIndexName(AppConstant.DynamoDBTableIndex.created_by_created_date_index);
queryExpression.setConsistentRead(false);
queryExpression.setRangeKeyConditions(filter);
queryExpression.setScanIndexForward(false);
return mapper.query(Expo.class, queryExpression);
As per above code i am getting all records created by me only. I want to fetch all records not created by me.
Also tried .withFilterExpression("created_by <> :val1").withExpressionAttributeValues(eav); but not working. As already question posted. Why is there no **not equal** comparison in DynamoDB queries?
and
DynamoDB: Filter Expression can only contain non-primary key attributes
The short answer is that you can’t fetch *all the items from a DynamoDB table in sorted order, by any attribute. DynamoDB just doesn’t work that way.
Think of DynamoDB as a distributed hash map of lists.
Just the same as you can’t expect to be able to get globally sorted results from such a map of lists, you can’t get them from DynamoDB either.
You can scan the whole table, and even filter, out some unwanted results as you go, but for sorting, you need to do it after you’ve fetched the records.
What you can do is retrieve items that have the same partition key, in order or the sort key.
And you can create an index where you pick an arbitrary attribute as the partition key and another as the sort key but even that approach has some limitations.
The best way to go is to really take some time and think about what you are going to do with the data. Why are trying to retrieve all items from the table in sorted order? Perhaps there is a better way to organize your data such the you din’t need to retrieve all of it.

Unable to use .startAt() with datetime in milliseconds

I have a very similar problem to this post - Firebase Query filtered by creation time and where date is greater than now
I have my dates stored in a "message" and I want to retrieve all messages after the current time.
This query works:
Query myTopPostsQuery = mFirebaseDatabaseReference.child(MESSAGES_CHILD).orderByChild("time");
this query returns no data:
Query myTopPostsQuery = mFirebaseDatabaseReference.child(MESSAGES_CHILD).orderByChild("time").startAt(System.currentTimeMillis());
This seems like it should work but from the docs I'm wondering if this is a data type problem?
In your query, you need to specify the full path to the child used for ordering:
orderByChild("date/time")
You indicate that your first query works. It may return the number of messages you expect, but if you look at them, you will find they are not ordered. The query processing is forgiving. If it doesn't find a value for the child identified by orderByChild(), it assigns a value of null and orders by these rules.

Firebase query implementation

If we query data from Firebase on a key for a child node, does it downloads the whole child node and filter data in the application or it downloads the query specific data? i.e. the filtered data
String myUserId = getUid();
Query myTopPostsQuery = databaseReference.child("user-posts").child(myUserId).orderByChild("starCount");
myTopPostsQuery.addChildEventListener(new ChildEventListener() {});
myTopPostsQuery will sort data according to starCount and I will receive data in the addChildEventListener() I want to know that whether this data is being filtered inside my app after receiving or my app downloads only the filtered data from the Firebase.
If you use a filter in your query you will download the query specific data without any operation executed on the client side.
Keep in mind that:
You can only use one order-by method at a time. Calling an order-by method multiple times in the same query throws an error.
You can combine multiple limit or range functions. For example, you can combine the startAt() and endAt() methods to limit the results to a specified range of values.
For any other info take a read here
You get the data already filtered. When you use a query, let say, limitToLast(10) you get only those 10 elements. This is happening also in your case with orderByChild("starCount"). In your SnanpShot will find only those filtered elements. Please visit official doc for more details.
Hope it helps.
Based on the code you pasted here, your query will just retrieve all the posts for the database path user-posts/<id> with an ordered manner which means that there is not filter. But still you will get back all the available posts under the path you are querying. It can be a "semi-filter" as it will find only the posts which include starCount field
The best thing is to filter during your query in order to retrieve back exactly what is needed and not everything as you are doing right now, imagine that this list of posts can be really big so you will have a big issue with performance later.
Read the following section here about sorting & filtering.
https://firebase.google.com/docs/database/admin/retrieve-data#orderbychild
Apart from that consider to add an index in userId field for speeding up your query.

How can I access the n-th item of a Firebase query object?

I try to sort my data with the following command:
Query queryref = mDBRef.orderByKey().limitToLast(100);
Now, I want access to the elements of queryref, but in a way that they are ordered from 1 to 100, the best would be some timestamp sort.
How can I do this in an efficient way (not too many databaserequests, good data structure, scalable)?

How to get an specific relation in a Parse.com table/class (Android)

Been trying to figure this out with no avail, let's say each row in a table/class has several relations, how can I reference an specific relation from that row?... so I can do something with the third relation from row 1, for example.
Thanks.
You would just use something like this:
ParseRelation relation = ParseObject.getRelation("relationName");
ParseQuery query = relation.getQuery();
Then from there you can filter out your query results like:
query.whereEqualTo("Name", "Luchito");
And if I'm understanding your question correctly, you could then just take the list returned from query and compare it against another relation using whereEqualTo or whereContainedIn.

Categories

Resources