Uncaught (in promise) Error: The query requires an index - javascript

I get this error once I added the Where method, but it works without the Where clause. In collection every document has Boolean called "status".
db.firestore().collection('jobs').where("status","==",true).orderBy("createDate").limit(10).get().then(querySnapshot =>{
})
})
All Help is Appreciated. Thanks!

Since you're querying on two fields (status and createDate), there needs to be a composite index on those two fields. Indices on individual fields are automatically created, but composite indexes are only created when you ask for them.
The error message should contain a link directly to the console to complete that task. If that's not the case, you can create it here.

Firestore query on more than two fields requires a composite index on that two filed.
So you have to create composite index status and createDate.You will get the error like following when you don't have an index.
ERROR Error: The query requires an index. You can create it here: https://console.firebase.google.com/project/admin-e8a7b/database/firestore/indexes?create_index=EgR0ZW1wGgcKA3VpZBACGg0KCXN0YXJ0ZWRBdBADGgwKCF9fbmFtZV9fEAM
at new FirestoreError (vendor.bundle.js:19925)
So when you click this link in error index will be created automatically.
You can also manually create index from firebase console
To combine the equality operator (==) with a range comparison (<, <=, >, or >=), make sure to create a custom index.

Related

Composite index firestore

I am using two different queries to order my data and putting a where clause. I need a composite index. I am also using limitToLast
const tradesRef = firebase
.firestore()
.collection("trades")
.orderBy("time", "asc")
.where("type", "==", "fiveMinutes")
.limit(15);
I have created a composite index such as:
I am still getting this error:
The fields indexed seem to be correct but you are not running a collection group query so have you tried setting the query scope to just "Collection" instead of "Collection group"?
Also try deleting current index and creating new index directly using the link in error.
As mentioned in the stackoverflow Answer :
Since you're querying on two fields (status and createDate), there
needs to be a composite index on those two fields. Indices on
individual fields are automatically created, but composite indexes are
only created when you ask for them. The error message should contain a
link directly to the console to complete that task. If that's not the
case, you can create it here.
For more information related to how firestore manages indexes, you can refer to the documentation.

Firestore says I have inequality filter on multiple properties, when I don't

I am trying to do a "small hack" to avoid reading the User document everytime the page loads. So I save it locally, everytime the page loads I get the local version, get the updated_at property and then do something like WHERE last_updated > {{updated_at}}. For that, I want to use this:
firebase.firestore().collection('User')
.where(firebase.firestore.FieldPath.documentId(), '==', firebase.auth().currentUser.uid)
.where('updated_at', '>', updated_at)
.get()
As you can see, I have one equality (==) and one inequality (>). Why do I get the following error on the console:
FirebaseError: Cannot have inequality filters on multiple properties: updated_at
at new t (https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js:1:47054)
at t.fromRpcStatus (https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js:1:116660)
at t.fromWatchChange (https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js:1:125914)
at t.onMessage (https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js:1:242411)
at https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js:1:241212
at https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js:1:241997
at https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js:1:144869
I am doing this to try to avoid reading from the database if the local version is the same as the one in the database. Maybe if you have a better way, please let me know.
Thanks
firebaser here
The equality check you have on documentId() is internally converted into a range check by Firestore, because the keys are stored as the last items in existing indexes (if I understand correctly). And that means that server-side you're trying to perform two inequality/range checks, which isn't allowed.
So the behavior you are seeing is correct. But it's definitely not intuitive, and the error message is also not helpful. We'll look for a way to improve the error message by detecting this combination.
I had the same problem and I implemented the following hack: I added the id as part of the field name on which I made the check for the latest version. If your logic allows you to do that, for you this would mean:
firebase.firestore().collection('User')
.where(id + '_updated_at', '>', updated_at)
.get()
This allows to bundle in just one where statement both the check on the id and on the date (documents with different ids wont have the field id + '_updated_at' and wont therefore be selected).
Worked like a charm for me

Firebase query requires an index - Boolean and Date

Currently i have a structure like this:
plannedDate:Timestamp,
deleted:Boolean
And i need to perform a query such as:
.where('plannedDate','>=',someDate)
.where('plannedDate','<=',someOtherDate)
.where('deleted','==',false)
As it should, based on firestore behaviour, it is asking me an Index to perform the query.
Have trying:
plannedDate Ascending delete Descending
and
plannedDate Ascending delete Ascending
So far it keeps asking me an index.
Detail: The link to create a new index, given by firebase exception, does not open. Probably firestore exception is still referencing an old way to do it. (js error is shown at console when opening the link)
So, question is, how to create an index to a query such as that?
Edit: Adding the link generated by the error
firebase generated link

How to remove a property from document - mongo / loopback

I have an old property on one of my models, and I'd like to remove it from all the documents in a collection. I've tried posting via /upsertWithWhere using the id to update by:
passing in undefined for the value which results in "http error 400 bad request"
passing in null which just sets the property to null
I also was thinking I could do a regular POST and just overwrite each document, but these particular documents are large and I'd rather not do that.
Is there a way to simply patch it?
Edit: Need an answer that implements this Via the Loopback API.
This query should do the trick:
db.collection('collection_name').update({},{$unset: {"old_property": ""}}, {multi:true})
Obviously, just make sure you insert into "old_property" the field name of that old property.
Explaining the query a little further...
"{}" matches all documents in the collection
"{$unset: {"old_property": ""}" removes the field(s) specified
"{multi:true}" (An optional field for update) Allows you to update multiple documents when set to true
Used this as a reference: https://docs.mongodb.com/manual/reference/method/db.collection.update/#multi-parameter

Filter by column to column sum or operator in BreezeJS

If I have a table with two numeric columns, say X and Y, and need to retrieve rows where say X > Y. How can I achieve this using BreezeJS? All examples I've come across are only capable of comparing columns to a fixed value so far.
Edit:
I tried using .where("Id", "==", "Id") just for testing and got the following error:
Query failed: A binary operator with incompatible types was detected. Found operand types 'Edm.Int32' and 'Edm.String' for operator kind 'Equal'.
To confirm that there were no errors with the query itself I tried .where("Id", "==", 1) and it went through.
Any clue on this anyone?
When Breeze executes a query, it checks the string on the right hand side of the predicate to determine if it is a property name instead of a literal and by default will chose the to treat the value as a property if a property of the same name exists on the type being queried. So if 'qtyOrdered' and 'qtyAvailable' are both properties of the 'Order' type then the following is supported.
var query = EntityQuery.from("Order").where("qtyOrdered", ">", "qtyAvailable");
This behavior can also be overridden ( sometimes necessary for string columns) but is rarely needed.

Categories

Resources