Fetching Object value in mongodb through query - android

I am making a chat app through socket.io in Android through Node.js . I want to store history of message and username which I have already stored successfully in mongodb but I want to fetch message:Object values here.. can someone please help me in writing query.
This query results in the output which you can see in the image.
messagesCollection.find().toArray().then(function (docs){
console.log(JSON.stringify(docs))
I want to retrieve "message" & "senderNickname" value through query. Replies are highly appreciated.

Pass projection operators in a query,
messagesCollection.find({},{_id: 0, "message.message":1,"message.senderNickname":1})
.toArray().then(function (docs){console.log(JSON.stringify(docs))});
make _id: 1 if you want object id as well for each record.

Related

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.

Having trouble creating a Firebase Query requesting only objects with a field of a certain value (in Android)

I have Firebase data that looks like this.
I want to create a Query object in java that retrieves only Invites (those are the objects with randomly generated key's KHcYy...) which have a name = "John Smith".
Here is the code for my current attempt, which is only giving me an empty query:
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.child("brother").equalTo("John Smith");
I tried taking a look through this tutorial on queries, but that only really had info on doing sorty and ranking based queries. I couldn't find any info on this type of task, and the API Reference didn't really make it clear what I'm supposed to be inputting in any of these calls.
Thanks for the help!
Have you tried this...
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.orderByChild("brother").equalTo("John Smith");

Cannot update the column information in table parse.com

I have a table lets call "Post" which has 3 columns "UserA"<ParseUser>, "UserB" and "Status"<String>.
Data consists of UserA currentUser, UserB another ParseUser, status => A
When I am going to update the status from "A" to "B" it throw me error
com.parse.ParseException: java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.
Can anyone help me please?
You need to be logged in as the user you're trying to modify. A possible solution would be to call a Cloud Code function and use the Master Key to do this.
The Cloud Code guide is here: https://parse.com/docs/cloud_code_guide
You should add below code to let afterSave / beforeSave hooks know that you are updating table directly.
if (request.master) {
Parse.Cloud.useMasterKey();
}

Android Parse query for data from more than two tables

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

Fetch Relational data from parse

I m working on parse. There are 2 table:
1. ScoreNode: User, score.
2. User: id, name, image(ParseFile)
Now need a query to fetch all the these info in one go.
ParseQuery<ScoreNode> query = new ParseQuery<ScoreNode>();
query.include("user");
How i can include image file?
query.include("user.image");
And query should be on scorenode.
regarding "scorenode.user"
if that is type pointer -> User object
and User has the image field
Then you should be able to get everything but the image in one query by tagging on '?include=User' to the query on scorenode.
In Rest api its just a parm '?include= $pointer'
dont know syntax in android SDK.
A 2nd call will be needed to get the bitmap image itself. I think you can get the image URL from the first query and then just use that url getting a bitmap.

Categories

Resources