1

The following code returning me a list of documents that have the field text.

db = TinyDB('/stream.json')
Tweet = Query()
db.search(Tweet.text.exists())

Instead I want to get only an array of the text field values over all the documents that have text field. It would be better to get it while reading the json instead of loading everything into the memory and then filtering it.

Nir
  • 2,497
  • 9
  • 42
  • 71

1 Answers1

2

Unfortunately, TinyDB only supports query filter. It doesn't support which fields to return in the documents that match that particular query filter, something similar to MongoDB projection parameter.

You can always use list comprehension to filter it yourself:

from tinydb import TinyDB, Query

db = TinyDB('/stream.json')

Tweet = Query()

texts = [tweet.get('text') for tweet in db.search(Tweet.text.exists())]
print(texts)  # List of all text field values.
Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57