I would like to know if there is a way to use an "AND" filter in a query on the parse server. In my case, I would like to filter posts by categories, for example, I have a colum "tags" (an array), and there, all the tags that are related to that post are. I retrieve the tags that the user is intersted with, and then I query to find posts that contain any of those tags ...
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
ArrayList<String> tags = (ArrayList<String>)object.get("myTags");
query.whereContainedIn("tags", tags);
The problem with this query is that it brings very vague answers, for example, a user follows a "Curiosities" tag, and also follows the tag "Avengers", I want to show it first, posts that contain these 2 tags, not only one of them.
whereContainedIn will find objects where a particular key's contains one of the elements in the passed array.
To get objects where a particular key contains all of the elements in a passed array, use whereContainedAll.
Note, though, if you're aim is to list matches in order, where objects matching all tags are listed first, followed by objects matching some tags. Then you'll need to query with the more permissive version (whereContainedIn) and do the sort -- sorting by matching tag count -- on the client.
According to Parse Server docs..
String[] names = {"Jonathan Walsh", "Dario Wunsch", "Shawn Simon"};
query.whereContainedIn("playerName", Arrays.asList(names));
https://docs.parseplatform.org/android/guide/#query-constraints
Related
Question
I have a collection named Users with a field named friendEmails which is an array that contains Strings.
I have a document with friendEmails = {joe#gmail.com, dan#gmail.com}, and I want to append newEmails = {mat#gmail.com, sharon#gmail.com} to it.
Problem
The only options I know for this are:
Reading the friendEmails array first, then adding the union of it and newEmails to the document.
Iterating over the elements of newEmails (let's and each iteration doing:
myCurrentDocumentReference.update(FieldValue.arrayUnion, singleStringElement);
(Since FieldValue.arrayUnion only lets me pass comma-separated elements, and all I have is an array of elements).
These two options aren't good since the first requires an unnecessary read operation (unnecessary since FireStore seems to "know" how to append items to arrays), and the second requires many write operations.
The solution I'm looking for
I'd expect Firestore to give me the option to append an entire array, and not just single elements.
Something like this:
ArrayList<String> newEmails = Arrays.asList("mat#gmail.com", "sharon#gmail.com");
void appendNewArray() {
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("Users").document("userID").update("friendEmails", FieldValue.arrayUnion(newEmails));
}
Am I missing something? Wouldn't this be a sensible operation to expect?
How else could I go about performing this action, without all the unnecessary read/write operations?
Thanks!
You can add multiple items to an array field like this using FieldValue.arrayUnion() as the value of a field update:
docRef.update("friendEmails", FieldValue.arrayUnion(email1, email2));
If you need to convert an ArrayList to an array for use with varargs arguments:
docRef.update("friendEmails", FieldValue.arrayUnion(
newEmails.toArray(new String[newEmails.size()])
));
How to make history score in Array
I try u make score in array like this
this my firestore
And this is my code
String uid = auth.getCurrentUser().getUid();
muridref.document(uid).update("nilai", FieldValue.arrayUnion(skortampil));
When I get the same score the array field doesn't make new Array data,
without see data there or not in array
As mentioned in the documentation Update elements in an array about this behavior:
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present.
Considering that, it's working as expected, since it's not adding values that are equal. So, this means that you won't be able to add values that are equal using the method arrayUnion() directly.
This other question from the Community - accessible here - indicates that for you to achieve this goal, you will need to read all the values from the array in your client side, update your values in the array outside the database and then, writing/updating it back in the database.
Let me know if the information helped you!
I am trying to do a simple Youtube Subcriber app.
When i try to send the user id , the returned result doesn't match the order i sent .
This is the api i am using
https://www.googleapis.com/youtube/v3/channels?part=statistics,brandingSettings,snippet&key=XXXXXXXXXXXXXXX&id=UCqIqRzEge-Qvt4Iglx-uKdQ,UC6bA7BuUmGKPIS3wW2voNxg,UC9JgYaC76Q_eSgYM01S5J_w,UCgefQJC5UgbWJHDxBqB4qVg,UCddiUEpeqJcYeBxX1IVBKvQ,UCOmcA3f_RrH6b9NmcNa4tdg
For example , i sent the data in this order
The Verge
Cnet
BfvsGf
The data returns :
BfvsGf
TheVerge
Cnet
It's not in order at all. Please advice.
Use Search:list, it returns a collection of search results that match the query parameters specified in the API request. By default, a search result set identifies matching video, [channel](https://developers.google.com/youtube/v3/docs/channels) and playlist resources but you ca also configure queries to only retrieve a specific type of resource.
HTTP request
GET https://www.googleapis.com/youtube/v3/search
include the order parameter, the parameter specifies the method that will be used to order resources in the API response.
You can sort list by descending order, sort from highest to lowest rating or sorted by alphabetically. The acceptable values are: date, rating, relevance, title, videoCount, viewCount. The 'order' parameter will sort results based on when the resources were created.
I have a Place object, containing an array with objectIds for Tag objects.
An example of an array:
[[{"__type":"Pointer","className":"FavoriteTag","objectId":"Toc5sVlzVd"},{"__type":"Pointer","className":"FavoriteTag","objectId":"cUxcl0IFFv"}]]
My first workflow way too slow:
query each Place object, get the array and parse it to find each objectId
get the corresponding Place with the objectId
The workflow I'm trying:
Find the objectId of each matching Tag object
Put these objectIds in a ArrayList
Get each Place object that contains a matching objectId from the arraylist
I don't know how to do step 3.
I'm trying:
parseQueryFavoritePlaces.whereEqualTo("tags", arrayListTagsObjectIds);
and
parseQueryFavoritePlaces.whereContainedIn("tags", arrayListTagsObjectIds);
but no luck so far.
Am I on the right track here?
Try giving this a shot:
query.whereContainedIn(String key, Collection<? extends Object> values)
According to the Parse documentation this adds a constraint to the query that requires a particular key's value to be contained in the provided list of values.
Here's the ParseQuery documentation if you think that might be useful!
Im trying to develop an application that accesses a database through POST requests and returns values in the database.
Im curious about something tho. what if i wanted to pass a number or a list to the database, such as the list of columns to grab for each row. Is it possible to extend the BasicNameValuePair class to acomplish this, or am i missing the correct way to do this?
Extending the BasicNameValuePair is not going to help you as the parameters of a HTTP request need to be name value pairs. If you want to pass more parameters, add more name value pairs and handle them in the server.
If the number of parameters are high, it would be prudent to use HTTP POST. If the parameters are going to be complex, it would beneficial to submit the request in a more user-friendly format like JSON or XML and parse it on the server side. However, if it is just the name of the columns of the table row, you can also think of using a format like comma separated list.
http://mydomain/getdata?id=10&columns=1,2,3,4
In case of the above, you need to parse the columns from the comma separated list at the server side, whereas you are creating name-value pair for columns as follows:
BasicNameValuePair columnData = new BasicNameValuePair("columns", "1,2,3,4");