Take

Take( num, collection )
Take( num, collection )
Take( num, collection )
Take( num, collection )
take( num, collection )
take num, collection
Take( num, collection )
Take( num, collection )

Description

The Take function returns a new collection of the same type that contains num elements from the head of the collection. If num is zero or negative, the resulting collection is empty.

When applied to a collection which is of type page, the returned page’s "after" cursor is adjusted to only cover the taken elements. As special cases:

  • If num is negative, after is set to the same value as the original page’s "before".

  • If all elements from the original page were taken, after does not change.

Parameters

Argument Type Definition and Requirements

collection

Collection

Take elements from this collection.

num

Integer

The number of elements to extract from the beginning of the collection.

Returns

A new collection of the same type with the first num elements.

Examples

The query below creates a new array collection containing copies of the first two elements of the collection’s argument.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{ "take": 2, "collection": [ 1, 2, 3 ] }'
client.Query(Take(2, Arr(1, 2, 3)));
System.out.println(
        client.query(
                Take(Value(2), Arr(Value(1), Value(2), Value(3))
                )
        ).get());
result, _ := client.Query(f.Take(2, f.Arr{1, 2, 3}))

fmt.Println(result)
client.query(Take(2, Arr(1, 2, 3)))
client.query(q.take(2, [1, 2, 3]))
$client.query do
  take 2, [1, 2, 3]
end
client.query(Take(count: 2, from: Arr(1, 2, 3)))
client.query(q.Take(2, [1, 2, 3]))
  .then((ret) => console.log(ret));
HTTP/1.1 200 OK
{ "resource": [ 1, 2 ] }
[ 1, 2 ]
[1, 2]
[1 2]
[ 1, 2 ]
[ 1, 2 ]
[ 1, 2 ]
[ 1, 2 ]
[ 1, 2 ]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!