I have table with many Users (for example 20 000). And I need to get 20 random user objects (with the condition, for example, “country” field == Spain).
How can I do this with backendless?
I have some ideas, but they are not good for my situation:
I can use counters and when I register new user, I can set "index" = totalCount - 1. And when I need a random objects, I just generate 20 random numbers and get all objects, where index equal to any of these random numbers. But this approach is not good, when I have condition country == Spain (so this approach is almost impossible)
I can get the collection of objects with my condition country == Spain. And from this collection I need to get random 20 objects. In this case, as far as I understand, I need to use offset when I get data from the collection (when I request a new page form the collection), but in this case I will need to perform 20 requests to backendless (with offset = random index and with page length = 1). So it will take too long to wait for responce.
Do you know any better approaches?
Hello, when I have 10 000 records with Spain, I will need to make 100 requests to get all the object id (because when I have a collection I can load only 100 records on one page). If I have 100 000 records, I will have 1 000 requests. And I think, that in this case user will need to wait about 3 or 10 minutes, and it’s too much for just getting 20 random objects.
- Can I load these 10 000 objectID with lower number of requests ( < 100 requests)?
- Are there any better algorithm?
- If there is no better algorithm, can you add proper method to your API in future, because I think that getting random objects is very important (it would be great to have a method to get objects with query and to set additional parameter if we need to get X number of random objects from all the objects that fit this query)?
Hi Pavel,
Would you be able to describe a use-case when you need to show random data (which satisfies some query parameter) in an application?
Regards,
Mark
Hi Mark,
I have 10 000 users, I need to get users only from country = Spain and status = GoodMood . I need to gain only random 50 users, because the app user needs to speak randomly with users by his search criterias (speak by chatting) .
After that I will need to gain users within distance (so I will need to get geopoints within this distance, and after that I will have query username in [@“username1”, “username2” …]. And all these users need to be Random.
Hope, it describes a use-case
Hello,
Is my explanation clear?
Will you be able to provide methods for getting Random objects? Or you don’t think that other users need it? (if you think so, I can provide you with several links where users, who user BaaS services try to find the solution to this problem. Please let me know, if you need these links)
Yes, it would be very helpful to see the links you are talking about.
I found this link only in one service (Parse). So if you implement the solution of getting Random objects, you will be the only BaaS service with this ability (and I think it’s great)
The links to the same questionhttps://www.parse.com/questions/is-it-possible-to-query-for-a-random-object
Did you figure out a way to get random objects from the database? I need this function also and it appears to be a feature that is not currently supported by BackendLess or any other provider.
David and Pavel,
We have been looking into adding this functionality. It seems feasible, however, a caveat is with “paging”. We limit number of returned objects per request to 100, that’s one page. When you request the next page and if the request calls for the randomization, it is possible that the subsequent request may return objects which were included in the previous pages. I think it is reasonable, since the server is stateless and there is no order to the retrieved objects. What do you think?
Mark
I was in need of 1 random object and I was able to get it as shown below. Hope this helps for someone who need to get 1 random object.
var rndm = Math.floor(Math.random() * totalObjects);
var dataQuery = new Backendless.DataQuery();
dataQuery = {
condition: "approved = 1",
options: { sortBy: "created desc", pageSize: "1", offset: rndm },
};
var remoteData = Backendless.Persistence.of(Object).find(dataQuery);
Thank you for putting it together, Purusothaman.
I would also like to have a way to retrieve data randomly ordered.
In my case I’m developing a quiz game, and I would like to get all the question (by paging) randomly ordered. At the moment I can only get “x” question, randomize them, add them to the adapter collection, get another “x”, do the same and so on… The problem is that the first “x” questions will be always the same, just in different order (same to the second “x” questions, etc)…
So if I want to make it look “more random” I have to increase x, which has an obvios impact on user experience.
Any suggestion?
Thanks!
Marcos.
Hello, having a rest query return a random record would be needful. I see this is an old topic. Is there any update about this in regards of the endpoint ? Like &sortBy=-random for example.
You can do it using the following algorithm:
- calculate the number of records using the COUNT function. I do not know if you use our SDK, the doc link is for REST.
- pick a random number between 0 and the number returned from step (1)
- Use the API retrieve objects where
pageSize=1
and offset=VALUE-FROM-STEP2
Regards,
Mark
Hello Mark,
You’re right (using Vuejs) that’ll do it. Thank you for the tip.
Regards,
xavier