Firebase Flutter - Request with multiple where arrayContains - arrays

In my firebase documents, I have a field named "tags" that is a List, for example tags = ["Amazing", "Great", "Disappointing"].
I want to filter the documents to query, so the user select a list of tags, for example filteredTags = [Amazing", "Great"].
In my request, I want to retrieve all documents that have all elements of filteredTags in there tags list.
This query does not work because it looks for a list within tags, which is just a list of string :
query = query.where(KeyTags, whereIn: filteredTags);
And this query return an error, because flutter does not allow to have multiple arrayContains in the same query (works if I have only 1 tag in filteredTags list) :
for(dynamic tag in filteredTags){
query = query.where(KeyTags, arrayContains: tag);
}
Finally, this one work but is not what I look for (it retrieves documents that have one of the filteredTags, whereas I want documents that have all of them :
query = query.where(KeyTags, arrayContainsAny: filteredTags);
Any idea of how to do it ?
Thanks !

What you're describing is an arrayContainsAll type operator, which doesn't exist at the moment.
The only way to implement this now is to store the tags as a map with subfields for each tag and then a value, and then query for those values with equality checks in your query. For example:
tags: {
"Amazing": true,
"Great": true,
"Disappointing": true
}
And:
query
.where("tags.Amazing", isEqualTo: true)
.where("tags.Great", isEqualTo: true)
Also see:
Firestore search array contains for multiple values
Firestore array contains query for list of elements
Firestore query - array contains all
Firestore: Multiple 'array-contains'

Related

PocketBase filter data by with multiple relation set

i have a collection where the field "users" contains the id's of two users.
how can i search for this dataset where this both user id's are.
i tried
users = ["28gjcow5t1mkn7q", "frvl86sutarujot"]
still don't work
This is a relation, so there must be a collection that allows you multiple, non unique results, so this table you are looking at to, is the the dataset, you can query the whole dataset on script with
// you can also fetch all records at once via getFullList
const records = await pb.collection('tlds').getFullList(200 /* batch size */, {
sort: '-created',
});
I sugest you to look into: js-sdk/pocketbase on github.

How to use arrayContains with list in FlutterFire that returns documents with ONLY the values in the given list

Example:
List<String> vehicleList = ['bus', 'plane']
var tarifRef = _firestore
.collection("vehicles")
.where("models", arrayContains: vehicleList );
Is there a way I can return documents that contain only the items in the vehicleList ?
What you're trying to do can only work with Firestore if the items in the list always have a predictable order. Typically you do this by making sure they are sorted by their natural sort order before you write the document, and in the list that you pass to the query. If the lists are identical, you can simply use a normal equality filter to compare them. If the lists contain the same strings, but they are not in the same order, then the equality check won't match them.
See also:
How do I query Cloud Firestore for an array that matches all values, but in any order?

How to write GQL `CONTAINS` comparator query in objectify

I use google cloud datastore to store and objectify to read the data. The entity that i use holds a list property and i would like to filter the records through multiple values and the results should be returned only if all the values are present in the list
GQL now supports CONTAINS query filter link here . I would like to know how this can be achieved in objectify
Assuming there are three entities that holds a list field
(Entity1) => list = ["id1","id2","id3","id4"]
(Entity2) => list = ["id1","id2","id3"]
(Entity3) => list = ["id1"]
queryList = ["id1","id2"]
How do i write an objectify query that uses contains. For example, ofy().load().type( Entity.class ).filter( "list contains" , queryList ).list(); so that only entities 1 and 2 are returned
I am familiar that IN filter ofy().load().type( Entity.class ).filter( "list in" , queryList ).list();
will return all three entities.
But the requirement is to fetch only entities that has both id1 and id2
You'll want to use an AND query, which should be ...filter("list", "id1").filter("list", "id2")

Querying mongoDB document based on Array element

This is one user's notes. I want to query and get only the notes of this use with "activeFlag:1". My query object code is
findAccountObj =
{ _id: objectID(req.body.accountId),
ownerId: req.body.userId,
bookId: req.body.bookId,
"notes.activeFlag": 1 };
But this query returns all the notes, including the ones with "activeFlag:0".
How do I fix this?
If you are on v2.2, use elementmatch operator. v3.2 and above allow aggregation and filter to return a subset of a document.
here is an example Retrieve only the queried element in an object array in MongoDB collection

Sitecore Solr Search by Multilist with Values from Another Multilist

I have a set of product items. Each product item has a multilist field that points to a set of product type items. When on a product page, I want to show a paged list of related items. These should be items that share a product type with the currently selected item. I'm running into some trouble because products can have multiple types. I need to split the type list on the current item and check that against the list of products in an expression. For some reason split and contains are throwing runtime exceptions and I can't really figure out why. I saw some things about the predicate builder being used for dynamic queries and I will try to use that with what I currently have but I'd like to know why this can't be done straight in the where clause.
Another issue I ran into is that the list of ids stored in solr are being stripped of their '{', '}', and '-' characters.
If you are already on the product page I assume you already have the product item and that product item should have a "ProductType" multilist field. You can use Sitecore.Data.Fields.MultilistFiled to avoid worrying about have to split the raw values.
You can then use Sitecore's Predicate Builder to build out your search predicate, which I assume you want to find all products that have one similar product type. You should adjust this search logic as needed. I am using the ObjectIndexerKey (see more here -> http://www.sitecore.net/Learn/Blogs/Technical-Blogs/Sitecore-7-Development-Team/Posts/2013/05/Sitecore-7-Predicate-Builder.aspx) to go after a named field, but you should build out a proper search model and actually define ProductTypes as a List< ID> or something similar. You may need to add other conditions to the search predicate as well such as path or templateid to limit your results. After that you can just execute the search and consume the results.
As far as Solr stripping the special characters, this is expected behavior based on the Analyzer used on the field. Sitecore and Solr will apply the proper query time analyzers to match things up so you shouldn't have to worry about formatting as long as the proper types are used.
var pred = PredicateBuilder.True<SearchResultItem>();
Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["ProductTypes"];
if (multilistField != null)
{
foreach (ID id in multilistField.TargetIDs)
{
pred = pred.Or(x => ((ID)x[(ObjectIndexerKey)"ProductType"]).Contains(id);
}
}
ISearchIndex _searchIndex = ContentSearchManager.GetIndex("sitecore_master_index"); // change to proper search index
using (var context = _searchIndex.CreateSearchContext())
{
var relatedProducts = context.GetQueryable<SearchResultItemModel>().Where(pred);
foreach(var relatedProduct in relatedProducts)
{
// do something here with search results
}
}
Just an Improvement to #Matt Gartman code,
The error (ID doesn't contain a definition for Contains) which keeps popping up is because .Contains is not a functionality of Type ID, I recommend you to Cast it in string type as below
foreach (ID id in multilistField.TargetIDs)
{
pred = pred.Or(x => (Convert.ToString((ID)x[(ObjectIndexerKey)"ProductType"]).Contains(id.toString())));
}

Resources