0

I have a database of tweets where the actual tweet text is in a field called "text".

it looks like this:

 "_id" : ObjectId("519184ad34dfb73dcdfdda2f"),
    "created_at" : "Mon May 13 01:41:25 +0000 2013",
    "id" : NumberLong("333758850945667072"),
    "id_str" : "333758850945667072",
    "text" : "No quiero ir a la. Escuela",

I was wondering how I could query and show the longest tweet? I've was thinking maybe .sort or .length or something like that but I've looked around and I haven't found anything that's worked so far.

Help! Thanks!

saya
  • 21
  • 1
  • 7
  • duplicated of https://stackoverflow.com/questions/29577713/string-field-value-length-in-mongodb – stenci Jul 17 '17 at 00:37

2 Answers2

2

If you need to perform the longest-tweet extraction then the best approach would be keeping a length of the tweet as a separate field in your document and having an index on it. Then you can do fast queries with .sort(-1).limit(1). You can write your own script for importing JSON data into MongoDB (http://docs.mongodb.org/ecosystem/drivers/) and add the length field on import.

Any alternative approach would imply a full collection scan in order to find a document with a maximum value of the 'text.length'. But this is a very resource-demanding operation and shall be definitely avoided unless effectiveness is not your concern.

0

While saving the document, save the length of the tweet also. You can optionally choose to index that field, so that query run too fast.

Abhishek Kumar
  • 3,328
  • 2
  • 18
  • 31