saya
saya

Reputation: 21

find longest string (longest tweet)? mongodb.

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!

Upvotes: 0

Views: 1506

Answers (2)

Alex Komyagin
Alex Komyagin

Reputation: 121

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.

Upvotes: 2

Abhishek Kumar
Abhishek Kumar

Reputation: 3366

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.

Upvotes: 0

Related Questions