Flutter Firebase where query - database

Lets imagine I have a list with Document ids e.g
List documentIds = ["Tj4EB68R1CwBYKPfWzcm","fExzUQbpzjEbGyE8mKDa","yPQvLLN7hkDFCjKN6mky"];
Now I want to get all Documents that match that id.
How can I do that?

You're looking for a whereIn query, which looks like this:
FirebaseFirestore.instance
.collection('yourdocuments')
.where(FieldPath.documentId, whereIn: documentIds)
.get()
.then(...);
The list of document IDs can be up to 10 long, so if you have more than 10 document IDs you'll need to run multiple queries and merge the results in your application code.
See also:
The FlutterFire documentation for querying Firestore
The reference documentation for Query.where()
The reference documentation for FieldPath.documentId.

Related

Is there any way to retrieve multiple documents by ID using FireStore (FlutterFire)?

In my flutter/dart app, I am using FlutterFire to sync data with my FireStore database.
I have a list of IDs from document (of the same collection) that I want to retrieve from FireStore.
I am trying to do the following:
await FirebaseFirestore.instance.collection('groups').doc([id1, id2, id3, id4]).get();
But ".doc()" won't accept a list as a parameter for the document ID to look for.
I tried with the "where" method like this:
await FirebaseFirestore.instance.collection('groups').where("id", whereIn: [id1, id2, id3, id4]).get();
But it won't work since the "id" isn't a field in the documents (it is their name/identifier).
The only two solutions I can think about are:
Doing one request per document I want to retrieve (not ideal at all)
Adding a field "id" in the documents that store the same information as their name/identifier (I don't like duplicating data that can end mismatching)
The third one (ideal IMO) would be to enable .doc to accept lists/arrays, so you can search for a list of documents you know the identifier of.
Am I missing something? Does anybody know any other solution?
THANKS!
(asked the same question here)
I found a solution here!
So adapting it to my case it looks like this:
await FirebaseFirestore.instance.collection('groups').where(FieldPath.documentId, whereIn: [id1, id2, id3, id4]).get();
Be aware that there is a limit of 10 to the number of values/IDs you can use in the search.

Where condition in firestore subcollection in ReactJS [duplicate]

This question already has answers here:
How to fetch all documents from a firebase collection where each document has some sub collection and in sub collection there is a document?
(2 answers)
Closed 6 months ago.
My data is stored in Firestore in below model.
My Problem is, I want to get schoolid of logged-in email id.
Data Model:
instituteData->schoolid->users->email->data
What I tried
const q = query(collection(db, "instituteData"), where("email", "==", "abc#gmail.com"));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
Screenshot of Data Structure
Screenshot of Data Structure
Firestore queries can only filter on data in the documents that they return. So unless the email field is. inside the school documents, your query won't work.
The common workarounds are to:
Replicate the email of each user in their school document, and using an array-contains query to then find the school.
Using a collection group query on all users collections to find the correct user(s), and then going up the ref.parent().parent() chain of the document snapshots to find the school for each result.
Also see:
firestore query document that have a collection contains a document
How to query Firestore subcollection's document's data?
How to fetch all documents from a firebase collection where each document has some sub collection and in sub collection there is a document?
As far as I know, there are no inner queries in firebase like we have in SQL, and your approach looks like the SQL table approach, clearly, your structure will not work well in firebase. You shouldn't make SQL-like structure in NoSQL, I learned this hard way.
one solution I can think of is to store the id of parent collections in child collection and all of their documents.
Basically, you are passing ids parent to all children. ¯\(ツ)/¯
In your case
you should store schoolId in all its students, then you can directly get that document with getDoc() query and schoolId will be there already.
Hope this helps 😁

Solr MoreLikeThis: Can I give Solr 5 document IDs and get more documents like these 5?

I'm unclear on this point from the documentation. Is it possible to give Solr X document IDs and tell it that I want documents similar to those?
Example:
The user is browsing 5 different articles
I send Solr the IDs of these 5 articles so I can present the user other similar articles
I am not clear about sending the document IDs, nor whether MoreLikeThis can operate on multiple documents as in this example.
you can try passing multiple Ids with the Query q=id:(document_id1 OR document_id2 OR document_id3) :-
e.g.
http://localhost:8080/solr/select/?qt=mlt&q=id:(document_id1 OR document_id2 OR document_id3)&mlt.fl=[field1],[field2],[field3]&fl=id&rows=10

Implementing keyword search on Google App Engine?

I'm trying to implement a keyword/tags search for a certain entity type in GAE's datastore:
class Docs(db.Model):
title = db.StringProperty()
user = db.StringProperty()
tags = db.StringListProperty()
I also wrote a very basic search function (using a fake list of tags, not the datastore values), that takes a query string and matches it to each set of tags. It ranks Docs based on how many query words match the tags. This is pretty much all I need it to do, except using the actual datastore.
I have no idea how to get the actual datastore values though. For my search function to work I need a list of all the entities in the datastore, which is impossible (?).
I also tried looking into GAE's experimental full-text search, and Relation Index Entities as a way to search the datastore without using the function I wrote. Neither was successful.
Any other ideas on how to search for entities based on tags?
It's a very simple query, if you need to find all Docs with a tag "findme", it's simply:
num_results = 10
query = Docs.all().filter("tags in", "findme")
results = query.fetch(num_results) # get list of results
It's well documented:
https://developers.google.com/appengine/docs/python/datastore/queries

Get facet results only in Solr

I am trying to make search on my database using Solr, and i need to build a facet for the date of the articles(2011-6-12,2011-7-1 ..etc) and another facet for category(sport, news..etc) i built my php code using apache_solr_service and every thing is fine till now, i can do search for my data in the database, but i want to use facet to filter the articles that are created in specific date or to get the articles that belong to a specific category,
i used:
http://localhost:8888/solr/collection1/select?facet=true&facet.query=datecreated:2011-6-21&facet.mincount=1&wt=json&json.nl=map&q=ruba&start=0&rows=10
its returned all the articles that have 'ruba' word and give me the count of articles that have been created in 2011-6-21.
what i need is to get only the articles that have ruba word AND are created on 2011-6-21, i want only facet results to be returned
Try using filter query, fq=datecreated:2011-6-21 instead of facet

Resources