So I have looked everywhere online and I just can't seem to figure out how to get this to work.
What I am trying to achieve is to find all users that have the same device under their devices. Here is the structure of the Firestore database: Database Image
So recently Firebase released version 5.3.0 of their Javascript library which in their release notes states:
Added 'array-contains' query operator for use with .where() to find documents where an array field contains a specific element.
Firebase Javascript Release Notes
I am trying to use this 'array-contains' in the .where() but i am getting no success. Here is my code:
admin.firestore().collection("users").where("devices",'array-contains', id)
//I get id from another place, i know that id is valid
This is the error i get back from the firebase functions log:
Unhandled error Error: Argument "opStr" is not a valid FieldComparison. Operator must be one of "<", "<=", "==", ">", or ">=". at Object.exports.(anonymous function) [as isFieldComparison] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/validate.js:89:23) at CollectionReference.where (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/reference.js:1095:18) at exports.checkIfDeviceIdValid.functions.https.onCall (/user_code/index.js:23:50) at /user_code/node_modules/firebase-functions/lib/providers/https.js:330:32 at next (native) at fulfilled (/user_code/node_modules/firebase-functions/lib/providers/https.js:25:58) at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Here are my dependencies from my package.json:
"dependencies": {
"@google-cloud/firestore": "^0.15.4",
"firebase-admin": "~5.13.0",
"firebase-functions": "^2.0.2"
}
If someone could help me with this and show me an example of how to do this I would greatly appreciate it. Thank you.
By default, Cloud Firestore retrieves all documents that satisfy the query in ascending order by document ID, but you can order and limit the data returned. In addition, you can retrieve all documents in a collection by omitting the where () filter entirely:
To read a single document, we can use DocumentReference ’s get () method that returns a Task < DocumentSnapshot >, while reading multiple documents from a collection or Query, we can use Firestore Query ’s get () method that returns an object of type Task < QuerySnapshot >. Both methods read the data only once.
When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document changes. Note: While the code samples cover multiple languages, the text explaining the samples refers to the Web method names.
Currently, this is only supported with one value, so don’t try chaining more than one of these in a single query. const col = firestore.collection('carts'); const query = col.where('items', 'array-contains', 'fruit loops') query.get(...)
Version 6.0.0 - August 9, 2018 of the Firebase Admin Node.js SDK have the array-contains
feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With