You can export right from the data collection, no need for a temporary collection:
for r in db.hello.find(fields=['text', 'timestamp']):
print '"%s","%s"' % (r['text'], r['timestamp'].strftime('%H:%M:%S'))
or to write to a file:
with open(output, 'w') as fp:
for r in db.hello.find(fields=['text', 'timestamp']):
print >>fp, '"%s","%s"' % (r['text'], r['timestamp'].strftime('%H:%M:%S'))
To filter out duplicates and print only most recent ones, the process should be split in two steps. First, accumulate data in a dictionary:
recs = {}
for r in d.foo.find(fields=['data', 'timestamp']):
text, time = r['data']['text'], r['timestamp']
if text not in recs or recs[text] < time:
recs[text] = time
and then output the dictionary content:
for text, time in recs.items():
print '"%s","%s"' % (text, time.strftime('%H:%M:%S'))