How can I get one document only by query in RethinkDB?
for an example, I want to get the document by this query below:
let searchQuery = {
name: 'rob'
}
var cursor = await r.table('users').filter(searchQuery).run(connection)
var user = await cursor.toArray()
console.log(user)
Result:
[ { email: '[email protected]',
id: '00e18124-714b-4298-aa34-5126ebda8828',
name: 'rob' } ]
Filter method actually returns a list of users
with the same name - 'rob'.
I want the result like get
method:
r.table('posts').get('a9849eef-7176-4411-935b-79a6e3c56a74').run(conn, callback);
Result:
{ email: '[email protected]',
id: '00e18124-714b-4298-aa34-5126ebda8828',
name: 'rob' }
But this method only takes document id.
Any ideas?
get
(by id) fetches a single row selection by design, butfilter
is a streaming operation that processes multiple values (like arrays and streams). You might want to usenth
that's designed to query for a stream/array element by its position, or its close alternative (). Here is an example:will return an array like
But the following queries
will both return
Note that both also throw
ReqlNonExistenceError: Index out of bounds: 0
in case if there are no rows found. However, you can easily fix it using thedefault
operator:Tested using RethinkDB data explorer with the "Raw view" tab active since both "Tree view" and "Table view" display single-element array as a single object. I'm almost sure the RethinkDB query engine is smart enough to make using
nth(0)
much more efficient rather than fetching the whole cursor to an array and taking the latter's first element in your code.