A no specific query using Mongo DB

A no specific query using Mongo DB
0

#1

I’ve downloaded a database yesterday consistent of tweets during the games in the confederation cup. And I saved it in the Mongo DB. My data model in the database is like the following json:

{ "_id" : ObjectId("51bc9036194069119ff88c10"),
  "text" : "Adianta AGORA ir para ruas e   protesta, como em Brasilia e outras capitais contra a copa das confederações e do... http://t.co/41e4GGoe4o",
  "created_at" : "2013-06-15 16:03:02",
  "id" : NumberLong("345934481724669954"),
  "user" : { "image" : "http://a0.twimg.com/profile_images/3425436378/57edc83f19d834283351a3729595d480_normal.jpeg",
             "screen_name" : "Fernando_Fontes",
             "id" : 54433693,
             "name" : "Fernando Fontes" 
           } 
}

Now, I wish to retrieve tweets using a term from the field ‘text’, for example: ‘Brasilia’. But, I couldn’t make a query searching for part of the text. I’m still starting with NoSQL and Big Data things. Is there any way to find the documents which has a word inside the field ‘text’?


#2

You can use a regex operator there ^^
https://docs.mongodb.com/manual/reference/operator/query/regex/

Something like:
db.collection.find({text:{$regex:/Brasilia/}})


#3

Yes this can be done with something called “text indexes”. I encourage you to read up on indexes here as they can be very useful. Indexing a schema field as a text index will essentially store each word in the text in an array.
Example:
“How to Index in MongoDB” would become [ "how", "index", "mongodb" ] . Notice how it ignores stop words such as “to”, and “in”.

To set up a text index using the model you provided it would look something like this if using the mongo shell:

db.collection.createIndex({ text: "text" })

Then to query the collection:

db.collection.find({ $text: { $search: "Brasilia" } })

Note that if the search text is “Adianta AGORA” it will return all docs that have either “Adianta” and/or “AGORA”. if you want to do an exact search you must wrap the search text in double quotes

db.collection.find({ $text: { $search: "\"Adianta AGORA\"" } })