Array-contains query not working react native [duplicate] - javascript

This question already has an answer here:
Firestore array-contains-any is not working properly
(1 answer)
Closed 3 years ago.
Hello i'm implementing the array-contain query but everytime i'm trying i'm Getting blank snapshot. My code is:
getStation = () => {
const query = firebase
.firestore()
.collection('routes')
.where('station', 'array-contains', 'station_name ');
query.get().then(snapshot => {
console.log("snapshot", snapshot);
snapshot.docs.forEach(doc => {
alert("eri")
console.log("eryh", doc.data);
})
})
.catch(err => {
console.log('Error getting documents', err);
});
}
I even tried to pass the static values but not able to get the desire value.

In your code there is a problem with typo mistake station must be stations
.where('station', 'array-contains', 'station_name ');
must be
.where('stations', 'array-contains', 'station_name ');
I hope it will fix your problem.
Update Answer
There is also typo mistake in station_name you just added the space after station_name
.where('station', 'array-contains', 'station_name ');
must be
.where('stations', 'array-contains', 'station_name');

You are trying to use array-contains to check for a field inside a map. This is not supported.
The array-contains can only search for a field inside the array and not a field inside a map which is inside that array.
A workaround I have tested is that you add an extra field inside the array that will be a boolean or a string and you can query based on it.
Using a boolean field in the array:
.where('stations', 'array-contains', true);
Using a String field in the array:
.where('stations', 'array-contains', 'station_name exists');

The array-contains operation checks if the precise object/value you specify exists in the array. It can't check on subsets of the array elements, only on complete values. So in your current structure, you'd have to pass all properties into the array-contains call:
.where('station', 'array-contains',
{ station_address_name: '...', station_lat_long: ..., status_name: '...' });
This typically is not feasible, in which case you'll want to store the elements that you want to filter on in a (additional) separate field in your documents. In this example, since you want to filter on station names, you'd add a field station_names where you store just the station names. Then you can query with:
.where('station_names', 'array-contains', 'name to search for')
Also see:
Firestore to query by an array's field value
Firestore array-contains-any is not working properly
How to make query spot only the some value in a Map inside an Array in flutter (dart)?

Related

How to query an array on a db firestore?

I don't think I'm very far from the answer, I tried to query an id inside an array on Firestore.
I wanna compare the id of the participants.
Also my DB in Firestore:
And also my function, for the moment my function return size =0 so they can't catch the query.
const deleteAbuse = (type) => {
const participants = channel?.otherParticipants;
if (!participants || participants.length != 1) {
return;
}
const myID = currentUser.id;
const otherUserID = participants[0].id;
console.log('id user', otherUserID);
channelDB
.where('participants.id', '==', otherUserID)
.get()
.then((querySnapshot) => {
console.log(querySnapshot.size);
querySnapshot.forEach((doc) => {
console.log(doc.ref);
//doc.ref.delete();
});
});
};
There is no way you can query filter your "channels" collection to get documents based only on a single field of an object that exists inside the participants array. In other words, you cannot create that filtering based only on that ID. To be able to filter the data, you should pass to the where() function, the entire object, and not only some partial data. The object should contain values for all the fields. Only the ID is not enough.
And also my function, for the moment my function return size =0
That's the expected behavior since in your array there isn't any object that holds only a single field called id. All objects hold 7 fields, or even more than that, as I cannot see all fields in your screenshot.
I wrote an article called:
How to update an array of objects in Firestore?
Where you can find in the first part, the simplest way to get a custom object from an array of custom objects.
Alternatively, you can create an array that can hold only the IDs of the participants, filter the documents and create another database call to get their corresponding data.

Firebase get all documents with specific input string [duplicate]

This question already has answers here:
Wildcard Firebase Query [duplicate]
(1 answer)
Google Firestore: Query on substring of a property value (text search)
(25 answers)
Closed 1 year ago.
let info = "every doc that starts with input preferable where i can place a limit on"
firebase.firestore().collection("usernames").doc(info);
I want to make a search bar where I get all users depending on the value I type in.
And I'm not sure how to do that exectly. basicaly I want to get all docs in the collection usernames that contain some input string
To start with, what you have done with the code above is create a document with an id of the string 'info'. Only one unique id could exist which would make querying unnecessary. In order to do a string search it would also be best to split the string into an array. II assume what you want to do is something like:
let info = "some string to go in document"
info = info.split(" ");
// Add a new document with a generated id.
firebase.firestore().collection("usernames").add({
info: info
})
.catch((error) => {
console.error("Error adding document: ", error);
});
Then you can query all documents in the collection to see if the info array contains a certain string word:
let check = "string"
firebase.firestore().collection("usernames").where('info', 'array-contains', check).get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
});
Hope this is helpful, its a difficult problem

Firestore query 'array_contains' pass in array [duplicate]

This question already has answers here:
Firestore search array contains for multiple values
(6 answers)
Closed 3 years ago.
I am looking at Firebase, cloud firestore and I am not understanding how I would perform a query and pass in an array.
const friendIds = [1, 2, 3];
Firestone.collection('conversations')
.where('members', 'array-contains', friendIds)
.get()
.then(querySnapshot => console.log(querySnapshot))
.catch(error => console.log(error));
anyone know how to achieve this?
This is currently not possible with a single query. array-contains can only find a single item in the named array.
Your alternative is to perform three queries, one for each item in the friendsId array, then merge the results in your code.
Not sure if this is possible, but here's an example from the Firebase Node.js Snippets that can give you an idea:
let citiesRef = db.collection('cities');
// [START array_contains_filter]
let westCoastCities = citiesRef.where('regions', 'array-contains',
'west_coast');
// [END array_contains_filter]
westCoastCities.get()
.then(res => {
console.log('West Coast get: ', res);
});
Reference Documentation
According to the official documentation regarding query operators:
You can use the array-contains operator to filter based on array values.
As I can see in your code, your friendIds argument that is passed as the third parameter to the where() function is of type array. What you are actually doing, you are searching in the members property which is of type array for an array, which is actually not possible since I assume the members array in your database contains numbers.
An query like this:
Firestone.collection('conversations')
.where('members', 'array-contains', 3)
.get()
.then(querySnapshot => console.log(querySnapshot))
.catch(error => console.log(error));
Will work perfectly fine because we are searching within the members array for a number.
If you need to query for all those numbers, you should perform three separate queries and combine the result client side.

Firestore where query with parameter not working

I have a query that is not working as I expect it to. I have a collection called 'games' that has a number of documents in it.
Each document has an array called 'hiScores' that consists of the results from that round. I would like to query all games for a specific player for example:
.where('hiScores.playerName', '==', 'David Lamm')
This is not returning anything. I have no problems when querying a top level variable in the document. What have I done wrong?
Database:
db.collection('games')
.where('hiScores.playerName', '==', 'David Lamm')
.get()
.then(function(querySnapshot) {
console.log('Davids games:')
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
}
);
The problem is that the hiScores field is an array, not an object. You can tell it's an array because the first immediate element of the array in the screenshot has index 0. You're trying to query hiScores as if it's a single object with a single property called playerName.
Firestore doesn't support querying for object properties within within array fields. If you have an array field, you can only query to find documents where an entire item is present in the array (an arrayContains query).
As pointed out by Doug this is currently not possible in firestore.
You can create a top level array of strings in each game named playerNames[]. Then use the where condition on playerNames with array_contains.

Firestore create compound query from array

I'm trying to create a compound query to do a range query.
let query;
category.queryParams.query.fieldFilters.forEach(function(fieldFilter) {
query = collection.where(fieldFilter.field, filterOperationString(fieldFilter.filterType), fieldFilter.filterValue);
});
But it's not giving me the results I want.
I'm having a feeling that the query needs to be created chained like collection.where().where()
If that is the case, how can I transform the contents of my array using that syntax?
From my understanding, you have an array named fieldFilters that contains multiple objects with a field attribute (e.g. 'price'), a filterType attribute (e.g. '>') and a filterValue attribute (e.g. 50). If you are trying to do multiple range queries on different fields, then this will not work as stated in the documentation https://firebase.google.com/docs/firestore/query-data/queries#compound_queries. Solutions do exist for this problem, I suggest you read Frank van Puffelen response to the following question: Firestore multiple range query.
Solved it with this code:
let query;
category.queryParams.query.fieldFilters.forEach(function(fieldFilter) {
if (typeof query === "undefined") {
query = collection.where(fieldFilter.field, filterOperationString(fieldFilter.filterType), fieldFilter.filterValue);
} else {
query = query.where(fieldFilter.field, filterOperationString(fieldFilter.filterType), fieldFilter.filterValue);
}
});

Categories

Resources