This is the layout of my firebase database.
MyCollection > MyDocument1 > 1: Data1
> 2: Data2
> 3: Data3
> 4: Data4
> 5: Data5
> MyDocument2
> MyDocument3
I'm trying to do the multiple fetch but having a difficulty in implementing it. What am I missing?
final FirebaseFirestore db = FirebaseFirestore.instance;
DocumentSnapshot<Map<String, dynamic>> documentSnapshot = await db.collection('MyCollection').where('MyDocument1', 'in',['1','2','3']).get();
This is the query you execute:
db.collection('MyCollection').where('MyDocument1', 'in',['1','2','3'])
In here you are looking for a field MyDocument1 in each document in MyCollection and then check whether its value is '1','2' or '3'.
Is that indeed what you want to do? If so, can you show a screenshot from the Firebase console of a document you expect to be returned?
If you just want to read MyDocument1 that'd be:
db.collection('MyCollection').doc('MyDocument1')
Related
I'm trying to fetch multiple documents but I'm unable to do it. I wanted to fetch multiple documents containing my search criteria.
Here's what I tried.
final FirebaseFirestore db = FirebaseFirestore.instance;
QuerySnapshot<Map<String, dynamic>> querySnapshot = await db
.collection('MyCollection')
.where('FldName', 'in', ['123', '345', '111']).get();
Error on the syntax which is pointing in the 'in':
Too many positional arguments: 1 expected, but 3 found.
Here's what my firebase looks like.
You need to use whereIn like this:
.where('FldName', whereIn: ['123', '345', '111']).get();
Not, in as a String.
The where query might be incorrect, try using this one that I got from the docs.
.where('FldName', arrayContainsAny: ['123', '345', '111'])
I want to update value of the field's document. I wrote a query but it doesn't work.
**//this query is working, I hava a doc Id**
final CollectionReference _company = FirebaseFirestore.instance
.collection('Company')
..where(FieldPath.documentId, isEqualTo: _auth.currentUser!.uid);
**// But this query is not working, because I have not doc** ID, its doc ID auto gen. ID in firebase
final CollectionReference _companyAdvert =
FirebaseFirestore.instance.collection('CompanyAdvert')..where('userId', isEqualTo: _auth.currentUser!.uid) ;
all the code here
To update a document field in firestore, you need to write
await FirebaseFirestore.instance
.collection('CompanyAdvert')
.doc(id)
.update({ 'profileImage': *new profile image* });
You must understand that to update a firestore document, you must keep a reference of the document id in the document itself. You can think of this as the primary key for the document.
There are two ways to do this.
1. Get a reference to the newly created document, and then get the id from the reference.
Then update the document with this id
2. Generate a random id locally and use that as the document id.
You can do this with the [Uuid package][1] on pub.dev
The first step goes like this:
// first, create a document using the add method
DocumentReference docRef = await FirebaseFirestore.instance
.collection('CompanyAdvert')
.add(*data*);
// then extract the generated document id
String id = docRef.id;
// then save it back to the document using
await FirebaseFirestore.instance
.collection('CompanyAdvert')
.doc(id)
.update({'id': id});
The second step goes like this:
String id = const Uuid().v4();
await FirebaseFirestore.instance.collection('CompanyAdvert').doc(id).set(*data*);
// Make sure you add the id as one of the fields to the map data
Note that the first step incurs a write operation which will count against your total quota for firebase. I recommend you use the second approach
Visit the FlutterFire documentation to learn more
When querying with where() in Firestore, it succeeds when I give the values one by one, but when I export a list, nothing returns.
Successful sample code:
List temp = example.sharedPreferences.getStringList(example.userList);
Firestore.instance.collection('example').where('id', whereIn: ['77','30','71','13']).snapshots(),
Failed sample code:
List temp = example.sharedPreferences.getStringList(example.userList);
Firestore.instance.collection('example').where('id', whereIn: [temp]).snapshots(),
What can I do in this situation?
temp is already a list therefore in the failed sample code, you don't need to use [], just do the following:
List temp = example.sharedPreferences.getStringList(example.userList);
Firestore.instance.collection('example').where('id', whereIn: temp).snapshots()
My database looks something like this:
Now as I start adding more and more data I will be getting variety of users. I want to extract data from one user based on their user ID. I tried using these codes but none of them worked. I am getting data in bulk of all the users but I just want one of them. Here's my attempt:
final data=await _collection.collection('UserDetails').getDocuments();
//print(user.uid);
DocumentReference ref = await _collection.collection('UserDetails').document(user.uid);
var lister=await ref.collection('Name');
print(lister);
This is the code for getting all their data:
for(var msgs in data.documents)
{
print(msgs.data);
}
I want a function or anything which could return data in this way:
function.giveUserID('uid').giveDataYouwanttoExtract('Attribute')
I can filter out using string conditions from all the data I am getting but as the database rises it will have to extract tons of data at once which will affect the performance and so I want to do this in this way. Please let me know if there's any way to just extract data of one user based on their uid or email or anything.
You can use queries. The code below returns all the users where name is equals to the Jack.
await _db.collection("UserDetails")
.where("Name", isEqualTo: "Jack")
.getDocuments()
.then((QuerySnapshot snapshot){
snapshot.documents.forEach((DocumentSnapshot documentSnapshot){
print(documentSnapshot.data);
});
});
I have linked my app with a firebase database and i am wanting to retrieve the string of one node from it.
The node I am wanting to retrieve is shown below with the name of 'timeStamp'. Is there a way i can retrieve this text and then print it?
The answer is covered in the Firebase documentation guide
Reading Data
and here's an example:
let ref = FIRDatabase.database().reference()
.child("Users+infomation/ff..etc/timeStamp")
ref?.observeSingleEvent(of: .value, with: { snapshot in
let val = snapshot?.value
print(val!)
})
*this is Swift 3