I am using Parse for one of my android application and struggling for a query to get output.
Application Parse DB has three table which is as described below-
User: user related information with unique object Id
Post: Contains Post related information with unique object Id and UserId(Pointer to user table) who create the post
Like: UserId(Pointer to user table) represent who is Liking the post, PostId(Pointer to post table) which post is liking and unique
object Id
Now I want to make a single compound query for All post for a specific user along-with total number of likes to corresponding post. Please suggest a solution/query for that
When a user creates a post, assign the user objectID to some post identifier property in the post table.
To retrieve all posts from a user, a query like this may help:
query.whereEqualTo("objectID", currentUser.objectID)
If you're just concerned about the like count, rather than each user who has liked the post, you may want to have an Integer value in the post table that is incremented after a user presses the like button on that post.
You should create your own webHook.
Following this exemples here you can easily learn and develop your own stuff, you can even add some triggers to your application!
https://parse.com/docs/cloudcode/guide#cloud-code-cloud-functions
https://parse.com/docs/cloudcode/guide#cloud-code-beforesave-triggers
If User table doesn't have Post<.Relation> column
I suggest you to read about how to join queries.
f.g. make a query1 to get the user, then another query2 to get all posts with whereMatchesQuery('UserColumnInPostTable', query1) function.
Now, you are going to get all posts of a specific user efficiently.
.
If User table have Post<.Relation> column
this is the easiest way
Just get the user object and get the relation object, then get the query, and find in background
Related
I currently have an app where I store user data in a SQLite database, and one of my fields is a User ID. I would like to add an option to auto-generate User IDs in an mmddyyXXX format, where XXX is a sequential number per user that resets every day.
Does anyone know how I would approach this? I looked at some of the other similar questions, but they don't seem to be helpful.
This is not complicated at all. If your'e similar with SQLite in android just take the date and the userId using a SELECT and generate that string yourself.
If the XXX is not the userId just save another table containing 'tokens' for users. every userId would have a 'token'.
Every new day just change the contents of this table.
I believe you could use a TRIGGER that will generate the userid when a row is inserted.
The following may suit :-
CREATE TRIGGER IF NOT EXISTS newuserid AFTER INSERT ON users
BEGIN
UPDATE users SET userid = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)||
(
SELECT CAST(substr('000',1,3-length(count()+1)) AS TEXT)||CAST((count()+1) AS TEXT)
FROM USERS
WHERE substr(userid,1,6) = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)
)
WHERE userid IS NULL;
END;
The trigger is named newuserid
userid is the column for the auto-generated id. The above relies upon it being NULL so it cannot be a PRIMARY INDEX.
There is no reliance upon other columns.
Testing
Starting with an empty table :-
Inserting 4 rows using INSERT INTO users VALUES(null,'This is a new user'); results in :-
To check for another date the rows are adjusted from 041018??? to 040918??? as per :-
4 more rows are inserted using INSERT INTO users VALUES(null,'This is a new user');, resulting in :-
Note this answer isn't intended to be fail-safe but rather the basis of the concept for the answer.
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.
I am building an Android app using Back4App(Parse) database.
I have two classes Post and Post_likes. I want to fetch all posts listing from Post table with individual post likes from post_likes table in the same query.
In Post table, I have post details like post_id, user_id and other details.
In Post_likes table, I have user_id who liked the particular post corresponding to post_id.
Post table with post details
Post_likes table with Post_id and user_id
Now, I want to get total post likes of corresponding post and users who like the particular post along with post listing.
I want to fetch total likes of particular post and then show in home screen post listing.
So, how to write the query to combine the two tables.
Any help will be deeply appreciated.
To define a Pointer Column in the DB, From the Back4App's dashboard you can click on "Edit" button for one Class and add a new Column. Select the type "Pointer" and select the Class name you want to point to in the combo list.
In your code simple add the pointer in the corresponding field
(Something like:
ParseObject PostLikes aPostLike;
ParseObject Post thepost
aPostLike.put("pointedPost", thepost);
(See documentation for accurate syntax).
When you query List of PostLike you can add a "where clause" directly on the pointer Field "pointedPost".
You can also include the content of the Post while query all PostLikes with the key words "include":
(see doc here :
http://parseplatform.org/docs/android/guide/#relational-queries ) :
ParseQuery<ParseObject> query = ParseQuery.getQuery("PostLikes");
// Include the post data with each comment
query.include("post");
I put my data in 3 tables(Links, Images and PDF)
each table has columns(university, faculty, grade, and description,...)
I want to retrieve description column in the 3 tables.
where university, faculty, and grade equal to certain values.
and sort them with creation date.
how can I perform that query in parse?
I'm not familiar with Android, but I'm pretty sure Parse does not support "Join" in the way a SQL database does. You could nest the queries, performing the next one in the previous one's completion block.
However, if you regularly want to get data from those 3 tables, I'd suggest you make it 1 table instead, with a column "Content" instead of Link/Img/PDF. Images and PDFs would probably be stored as PFFiles anyway, and you can put link as either its own string column or putting it in a file. You could also add a column "type" if you want to be able to query a specific type, or just keep track of which columns contains which data.
Then you could query the "Content" class, on the keys you want.
I think this link might help you
https://parse.com/docs/js/guide#relations and it is quite simple and nicely explained . You can't do it directly in the database, though.
I'm new to greenDAO and I'm working on writing the DaoGenerator. One issue that I've run into is that I have a user table and a wallpost table. I would like to be able to have two columns in the wallpost table that are toMany relations to the user table (the wall owner and the posting user) they may or may not be the same user, but so far it doesn't look like it is possible to have two toMany relations that point to a single table in the same table.
Is there a better way to do this/a way to make this possible? I'm hoping to be able to load the wall posts and fetch the wall owner and posting user by calling .getOwner() and .getPoster().
Thanks
You must set names for the relations. Have a look at the (just improved) section called Relation Names and multiple Relations of the documentation on relations. It comes with an example:
Property pictureIdProperty = user.addLongProperty("pictureId").getProperty();
Property thumbnailIdProperty = user.addLongProperty("thumbnailId").getProperty();
user.addToOne(picture, pictureIdProperty);
user.addToOne(picture, thumbnailIdProperty, "thumbnail");