Min

Min( value_1, value_2, ... )
Min( value_1, value_2, ... )
Min( value_1, value_2, ... )
Min( value_1, value_2, ... )
min( value_1, value_2, ... )
Min( value_1, value_2, ... )

Description

The Min function returns the smallest value in a list of values.

Types have an order of precedence. When comparing values of different types, they are ranked in the following order, from least to greatest.

  1. Number (integers and decimals: 0.5 < 1 < 1.5 < 2)

  2. Byte

  3. String

  4. Array (ordered lexically, like strings)

  5. Object (ordered lexically, like strings)

  6. Reference

  7. Timestamp

  8. Date

  9. Boolean (false < true)

  10. Null

With this precedence, Numbers are always smaller than Strings.

The run time of Min is dependent on the number of elements in the underlying set or page — it’s linear, or O(n). For very large sets or pages, executing Min might result in a query timeout error, or "width" error.

For query "width" errors, the underlying set or page involves more than 100K items. This can happen when using a set function, such as Difference, where more than 100K items need to be considered to produce the set that Min evaluates. To resolve this, use Paginate to limit the set or page size.

For example, instead of:

Min(
  Difference(
    Match(Index("Index1"), "term1"),
    Match(Index("Index2"), "term2")
  )
)

use:

Min(
  Paginate(
    Difference(
      Match(Index("Index1"), "term1"),
      Match(Index("Index2"), "term2")
    ),
    { size: 10000 }
  )
)

This does mean that if the entire set must be evaluated to arrive at the correct result, you would have to page through the Paginate results.

For query timeout errors, you may specify a larger query timeout via the driver that you are using.

Parameters

Parameter Type Definition and Requirements

value

List of Values

A single Value or a list of Values.

Returns

A value which is the minimum value from the value list.

Examples

The following query executes an array of independent min operations and returns the results in an array. The result array position matches the execution array position. The top operation in the execution array, minimum of the values 1, 5, and 22, returns a long value of 1 in the top position of the result array.

try
{
    Value result = await client.Query(
        Arr(
            Min(1, 5, 22),
            Min(1, 0, 3, -1),
            Min(-1, 12, 3, -1),
            Min(Arr(10))
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(LongV(1), LongV(-1), LongV(-1), LongV(10))
result, err := client.Query(
	f.Arr{
		f.Min(1, 5, 22),
		f.Min(1, 0, 3, -1),
		f.Min(-1, 12, 3, -1),
		f.Min(f.Arr{10}),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[1 -1 -1 10]
System.out.println(
    client.query(
        Arr(
            Min(Value(1), Value(5), Value(22)),
            Min(Value(1), Value(0), Value(3), Value(-1)),
            Min(Value(-1), Value(12), Value(3), Value(-1)),
            Min(Arr(Value(10)))
        )
    ).get());
[1, -1, -1, 10]
client.query(
  [
    q.Min(1, 5, 22),
    q.Min(1, 0, 3, -1),
    q.Min(-1, 12, 3, -1),
    q.Min([10]),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ 1, -1, -1, 10 ]
result = client.query(
  [
    q.min(1, 5, 22),
    q.min(1, 0, 3, -1),
    q.min(-1, 12, 3, -1),
    q.min([10]),
  ]
)
print(result)
[1, -1, -1, 10]
[
  Min(1, 5, 22),
  Min(1, 0, 3, -1),
  Min(-1, 12, 3, -1),
  Min([10]),
]
[ 1, -1, -1, 10 ]
Query metrics:
  •    bytesIn:  71

  •   bytesOut:  25

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 4ms

  •    retries:   0

The following query uses the same approach as the previous query to demonstrate using Min with various types of values:

try
{
    Value result = await client.Query(
        Arr(
            Min("A", "B", "C", "D"),
            Min(10, 11, "A"),
            Min(Time("1970-01-01T00:00:00Z"), Time("1980-01-01T00:00:00Z")),
            Min(Date("1970-01-01"), Date("1930-01-01")),
            Min("A", 1),
            Min(true, false),
            Min(Obj("x", 10), Obj("x", 11)),
            Min(Arr("A"), Arr("B"), Arr("C")),
            Min(Arr("X"), Arr("A", "B"))
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(StringV(A), LongV(10), FaunaTime(1970-01-01T00:00:00Z), FaunaDate(1930-01-01 12:00:00 AM), LongV(1), BooleanV(False), ObjectV(x: LongV(10)), Arr(StringV(A)), Arr(StringV(A), StringV(B)))
result, err := client.Query(
	f.Arr{
		f.Min("A", "B", "C", "D"),
		f.Min(10, 11, "A"),
		f.Min(f.Time("1970-01-01T00:00:00Z"), f.Time("1980-01-01T00:00:00Z")),
		f.Min(f.Date("1970-01-01"), f.Date("1930-01-01")),
		f.Min("A", 1),
		f.Min(true, false),
		f.Min(f.Obj{"x": 10}, f.Obj{"x": 11}),
		f.Min(f.Arr{"A"}, f.Arr{"B"}, f.Arr{"C"}),
		f.Min(f.Arr{"X"}, f.Arr{"A", "B"}),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[A 10 {0 62135596800 <nil>} {0 60873292800 <nil>} 1 false map[x:10] [A] [A B]]
System.out.println(
    client.query(
        Arr(
            Min(Value("A"), Value("B"), Value("C"), Value("D")),
            Min(Value(10), Value(11), Value("A")),
            Min(Time("1970-01-01T00:00:00Z"), Time("1980-01-01T00:00:00Z")),
            Min(Date("1970-01-01"), Date("1930-01-01")),
            Min(Value("A"), Value(1)),
            Min(Value(true), Value(false)),
            Min(Obj("x", Value(10)), Obj("x", Value(11))),
            Min(Arr(Value("A")), Arr(Value("B")), Arr(Value("C"))),
            Min(Arr(Value("X")), Arr(Value("A"), Value("B")))
        )
    ).get()
);
["A", 10, 1970-01-01T00:00:00Z, 1930-01-01, 1, false, {x: 10}, ["A"], ["A", "B"]]
client.query(
  [
    q.Min('A', 'B', 'C', 'D'),
    q.Min(10, 11, 'A'),
    q.Min(q.Time('1970-01-01T00:00:00Z'), q.Time('1980-01-01T00:00:00Z')),
    q.Min(q.Date('1970-01-01'), q.Date('1930-01-01')),
    q.Min('A', 1),
    q.Min(true, false),
    q.Min({ x: 10 }, { x: 11 }),
    q.Min(['A'], ['B'], ['C']),
    q.Min(['X'], ['A', 'B']),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[
  'A',
  10,
  Time("1970-01-01T00:00:00Z"),
  Date("1930-01-01"),
  1,
  false,
  { x: 10 },
  [ 'A' ],
  [ 'A', 'B' ]
]
result = client.query(
  [
    q.min("A", "B", "C", "D"),
    q.min(10, 11, "A"),
    q.min(q.time("1970-01-01T00:00:00Z"), q.time("1980-01-01T00:00:00Z")),
    q.min(q.date("1970-01-01"), q.date("1930-01-01")),
    q.min("A", 1),
    q.min(True, False),
    q.min({"x": 10}, {"x": 11}),
    q.min(["A"], ["B"], ["C"]),
    q.min(["X"], ["A", "B"]),
  ]
)
print(result)
['A', 10, FaunaTime('1970-01-01T00:00:00Z'), datetime.date(1930, 1, 1), 1, False, {'x': 10}, ['A'], ['A', 'B']]
[
  Min('A', 'B', 'C', 'D'),
  Min(10, 11, 'A'),
  Min(Time('1970-01-01T00:00:00Z'), Time('1980-01-01T00:00:00Z')),
  Min(Date('1970-01-01'), Date('1930-01-01')),
  Min('A', 1),
  Min(true, false),
  Min({ x: 10 }, { x: 11 }),
  Min(['A'], ['B'], ['C']),
  Min(['X'], ['A', 'B']),
]
[
  'A',
  10,
  Time("1970-01-01T00:00:00Z"),
  Date("1930-01-01"),
  1,
  false,
  { x: 10 },
  [ 'A' ],
  [ 'A', 'B' ]
]
Query metrics:
  •    bytesIn: 316

  •   bytesOut: 108

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 6ms

  •    retries:   0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!