limit amount of objects in array of postgresql jsonb_agg

André

I have this query:

SELECT jsonb_agg(jsonb_build_object('me_id', me_id)) from message

and got the following result:

[{"me_id": 2064}, {"me_id": 2065}, {"me_id": 2066}, {"me_id": 2067}, {"me_id": 2068}, {"me_id": 2069}, {"me_id": 2070}, {"me_id": 2071}]

so far so good. However I want to LIMIT the amount of objects in the array. For instance to 3. But adding LIMIT 3 is not working.

How can I limit the amount of objects within the array?

EDIT: @GMB's answer solved it. Thank you!

GMB

You would need a subquery:

select jsonb_agg(jsonb_build_object('me_id', me_id)) 
from (select * from message order by me_id limit 3) t

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Array of objects in postgresql

Postgresql query array of objects in JSONB field

How to put a limit on the amount of objects in an ArrayList

Adding unknown amount of objects to Array via function

insert jsonb data in postgresql, option array or objects, valid way

Filtering unique names of an array with massive amount of objects

Postgresql get keys from array of objects in JSONB field

Search by date in array of objects within PostgreSQL JSONB Column

Limit query by sum of attributes of objects in Rails and Postgresql

Can PostgreSQL JOIN on jsonb array objects?

PostgreSQL - Add key to each objects of an JSONB array

Use data to define amount of objects in Array

Filter array of objects by amount property

How to iterate over PostgreSQL jsonb array of objects and modify elements?

PostgreSQL query on a JSONB Array of Objects

Update array of objects in JSONB format PostgreSQL

How do i limit the amount of objects are instantiated?

PostgreSQL aggregate function calls cannot be nested using jsonb_agg function

Postgresql Using array_agg and jsonb_build_object

Postgresql get keys from nested array of objects in JSONB field

Update the value of an attribute in a postgresql jsonb array of objects across muliple records

push the total amount of objects to an array

Limit List's objects amount - SwiftUi

Postgresql remove object from jsonb array of objects by key value

Constructor that receives an arbitrary amount of objects or an array of objects

PostgreSQL: Does jsonb sort Objects in array?

How to merge a JSONB array of objects into a single object in PostgreSQL

How to group by the amount of values in an array in postgresql

How could I limit the amount of items in an array (js)

TOP Ranking

HotTag

Archive