Firebase query requires an index - Boolean and Date - javascript

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

Related

how to Retrieve firebase database start from bottom to up?

I have a set of items in firebase realtime,
when i Retrieve data the old data appears at the first.
Already i tried
Firebase.database().ref().child('chains').orderByKey().startAt();
but the data didn't appear
I want upload new item and it appear at the first how do i do this?
this my code:
this my database
How do you determine which items is newer? Is there any child node containing timestamp? If yes then you should use orderByChild():
firebase.database().ref('chains').orderByChild('addedAt')
However this will sort by ascending order and hence oldest items will be retrieved first. If you are looking for latest 50 items then you can use limitToLast method and then reverse the order using Javascript:
firebase.database().ref('chains').orderByChild('addedAt').limitToLast(50)
If you are ordering them using the item keys then you can use orderByKey but to get items in descending order i.e. newest first, you'll have to use same logic with limitToLast.

How to load data onScroll in ReactNative with Firebase Realtime Database

i want to load data from my realtime database, but only 15 entries everytime, because the database is huge. The database has a name of the vocable and got information about it like translations and stats. I want to sort it alphabetic by the value "wordENG", but there is a problem, when i use orderByChild like this:
database()
.ref(`vocables/${UID}`)
.orderByChild("wordENG")
.startAt(requestCount)
.limitToFirst(15)
.once("value")
.then(snap => {
console.log(snap.val());
})
When i try to use startAt, to get the data on scrolling, i get the problem that startAt need to be a string, so a word of the database list. I don't want to store this word everytime and search for new one after that, but currently i cannot see another way. Is there a way to get data alphabetic on scrolling with a number to count or do i need to realize it with saving the last word and search from there?
Pagination with Firebase queries work based on knowing the anchor item, not on offsets. So you will need to indeed know the wordENG value of the node to start at (or start after with the relatively new startAfter method), and possibly the key (if there may be multiple child nodes with the same wordENG value.
If you're new to Firebase, I recommend also reading some of the previous questions about pagination, as this comes up regularly.

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

How to remove all items of a Mongo collection

I have seen that to remove all items of a Mongo collection using JavaScript I should use :
DockerStats.remove(); //where DockerStats is my collection
So my goal is to purge the DB every 20sec so I did the following code :
setInterval(Meteor.bindEnvironment(function(){
DockerStats.remove();
console.log("ok")
}),20000);
But when I start the app I had +/- 1000items then despite the terminal wrote 2 times "ok" I still have more than 1000items so it doesn't work because even if I check right after the "ok" I have more than 1000items and the number is always growing up.
So maybe I'm removing the items with the wrong way ?
According to the docs, you need to pass in an empty object to delete the whole collection. So, the below would remove all students from the Students collection:
Students.remove({})
I think this is because if you want to remove everything and start over you would use the drop method and recreate it, which the docs says is more performant.

Why I cannot get the correct result through IDBKeyRange.bound function in IndexedDB?

I've got a mock up indexedDB holds items like:
{
id:1002,
name:"Frank",
company: "dg",
age:30
}
And I've created the index ['name','age','company'] , just want to get the result I need like websql do.
getMultipleDataByRange function defined to get items, I set ['0',24,'ef'] as lowerBound and ['z',24,'ef'] as upperBound, I thought it should show me the result match the items who's 24 years old plus whose company is "ef". But the result shows that all the items meet the range I set. How can I set the range to get the correct result.
Code can also be checked by JSBIN link I generated:
http://jsbin.com/sinuxa/edit?js,console
It'll work if you change the order of your index so that the part that varies (name) is at the end. If you want to know why, see In IndexedDB, is there a way to make a sorted compound query? for an explanation of how compound indexes work in IndexedDB.

Categories

Resources