Skip to main content

Get Random Object(s)

Random list of objects

You can query the specified number of objects randomly by calling the getRandom method.

note

It retrieves the specified number of objects from the database randomly. If filter modifier is used with this method, Altogic first narrows down the set of objects that can be selected using the filter query and among these filtered objects performs random selection.

tip
  • Applicable query modifiers
    ✓ filter
    ✓ lookup
    ✓ omit
let count = 3;

// Gets the random 3 quotes where author equals to "Salvador Dali"
let result = await altogic.db
.model("quote")
.filter("quote.author == 'Salvador Dali'")
.getRandom(count);
Example Response
{
"data": [
{
"_id": "62373335b9a84d607cd78e85",
"author": "Salvador Dali",
"content": "Intelligence without ambition is a bird without wings."
},
{
"_id": "623732ed1182562412d85cde",
"author": "Salvador Dali",
"content": "Life is too short to remain unnoticed."
},
{
"_id": "623732ed1182562412d85cde",
"author": "Salvador Dali",
"content": "It is either easy or impossible."
}
],
"errors": null
}

Parameters

Here you can find parameters for the getRandom method.

#

Name

Data type

Required

Description

1countnumberYesAn integer that specifies the number of items to randomly select.
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.