Hi, I’m trying to save an array of objects that contain tweets from a Twitter account. That array has the tweets in chronological order. When I use Backendless.Data.of( “Table” ).bulkCreate( ArrayOfObj ) the tweets get stored, but not in the same order.
I need to access the database so I can ask for the last item stored, which should be the last tweet in chronological order, but it sends a random one. I’m assuming that bulkCreate() doesn’t respect the order of the array it’s saving?
If that is the case, I do store the tweet publication date. I can’t find how to sort by the publication date and retrieve just the last one (not all the items sorted).
I’d appreciate any help!
The order in which the objects are stored is not guaranteed. I recommend that you create a separate property/field in each object that would contain the time of the tweet post. When you retrieve the objects, you can request that the database sorts the objects by that property.
Thank you Mark. The question then is, if I have the database sorted by the time property, can I retrieve just one, not all of the ordered list? I mean, the first in descending order…
Yes, are you doing it with Codeless?
No, sorry I didn’t mention it. I’m using Typescript.
That’s not a problem. To get your object, run a
find API request with
pageSize of 1 and use the sorting option to get the objects sorted as you need. You will get back a collection of 1 object, so make sure to “take it out of the collection”.
Hope this helps.
Thank you very much Mark, I’ll try that.
Just in case anyone needs this information, this is how I solved based on what Mark told me:
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setPageSize( 1 ).setOffset( 0 ).setSortBy( [“date_published DESC” ] );
Then I use
let result = await Backendless.Data.of( “My table” ).find( queryBuilder )
This did the trick. Thx again Mark.
You’re welcome, @Carlos_de_la_Lama-Noriega . Please keep in mind that
result is a collection with size of 1. Make sure to get the first element of the collection to get the actual object.
You can also use the
findFirst method instead of just
find, then it will return you just the actual object, not within the list.