I want to query all data by _id in fields receiver ( as image below), I use firestore firebase. Can anyone help me?
If you want to query documents for values nested in map fields, you can use dot notation to find that value:
firestore.collection("notifications").where("receiver.id", "==", x)
Related
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.
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 😁
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.
I am studying Firestore with React.
After I added the array data into the firestore, I am trying to count the certain element in array of data in the document.
Like the picture I attached, there are several documents includes array data.
I want to count the number of CS 34800 in class array from all documents.
What is the best way to get it?
Thank you!
enter image description here
There are no counting or aggregating operations on Firebase, so that way you have it now, you will have to query for all the documents that match your criteria, then count the number of documents in your result set.
You can use an "array_contains" operator to get all the documents where an array contains some value.
collectionRef.where("class", "array-contains", "CS 34800").get()
.then(querySnapshot => {
const count = querySnapshot.size
});
If you need to get a count without making a query for documents, you'll have to maintain that yourself for every string that you could possibly need to count, which is an entirely different problem.
I have data in which field have following java data types.
What would be the best way to index such kind of data.
Thanks,
field_a map<string,string>
field b map<string,array<string>>
How to define schema.xml for it
Currently Solr doesn't support map type field type. So, you can not query on some particular key inside the map and retrieve its value. I don't know whether it'll be helpful you or not, but I can suggest you a way to keep this in Solr.
You can store the map in a field as a json formatted string. Say, document1 has map1 in field_a and document2 has map2 in field_a. Now, you keep some distinct data related to each map to their corresponding documents. When you want to query, query on those fields in stead of the maps. Then in the search result, when you retrieve the json formatted string, parse it in your application and get the values.
Hope this will help.