W3cubDocs

/RethinkDB Python

ReQL command: count

Command syntax

sequence.count([value | predicate_function]) → number
binary.count() → number
string.count() → number
object.count() → number
r.count(sequence | binary | string | object[, predicate_function]) → number

Description

Counts the number of elements in a sequence or key/value pairs in an object, or returns the size of a string or binary object.

When count is called on a sequence with a predicate value or function, it returns the number of elements in the sequence equal to that value or where the function returns True. On a binary object, count returns the size of the object in bytes; on strings, count returns the string’s length. This is determined by counting the number of Unicode codepoints in the string, counting combining codepoints separately.

Example: Count the number of users.

r.table('users').count().run(conn)

Example: Count the number of 18 year old users.

r.table('users')['age'].count(18).run(conn)

Example: Count the number of users over 18.

r.table('users')['age'].count(lambda age: age > 18).run(conn)
r.table('users').count(lambda user: user['age'] > 18).run(conn)

Example: Return the length of a Unicode string.

> r.expr(u'こんにちは').count().run(conn)
5

Related commands

Get more help

Couldn't find what you were looking for?

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/python/count/