Reputation: 43
How do i check one Field in all Documents in a Collection against a Value?
I am setting up a digital tally for my coworkers to track what we take out of the fridge. we all have a 4 digit number on our Cards we would like to use to log in and then store what we took out of the company fridge (and pay up at the end of the month)
I have one Collection that contains the Users Name and their Number. When i enter my Code i want to check the entire Collection if that entry exists.
My Firebase with Users and ID (nummer)
Following this this post Check if value exists in firebase DB i have my code set up like this:
ref.child("nutzer").orderByChild("nummer").equalTo("1337").once("value",snapshot => {
if (snapshot.exists()){
const userData = snapshot.val();
console.log("exists!", userData);
}
else{
console.log("does not exists!");
});
when running the code i get an Error "ref.child is not a function". trying to figure out why i get this error i found this post: "ref.child is not a function" when adding .orderByChild.StartAt filter but was unable to find a solution to my inital question.
Kind regards.
Upvotes: 4
Views: 7948
Reputation: 2105
The code you have used is from firebase realtime database, but your data is stored in firebase firestore (firebase provides two different types of databases)
Firestore has recently added collection group queries which can search across multiple collection documents, e.g.
var nummers = db.collectionGroup('nutzer').where('nummer', '==', '1337');
nummers.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id, ' => ', doc.data());
});
});
To test whether the data exists, you could simply date the initial query snapshot before processing and check the .empty
property. E.g.
nummers.get().then(function (querySnapshot) {
return !querySnapshot.empty
})
More information about querying in firestore can be found at: https://firebase.google.com/docs/firestore/query-data/queries
Upvotes: 8