Text searching in mongo

Write a mongo query for text searching.

After completing Indexing in mongo you can apply search operation on that text field.

Example:

[{ 
    "_id" : ObjectId("60221f914cff3730adcdf8e1")
    "mediaSite" : "facebook"
    "date" : ISODate("2021-02-05T18:30:00.000+0000")
    "message" : "Hi Sohan I waiting near about RamChowk please call me back"
}
    "_id" : ObjectId("6022206b4cff3730adcdf8e5")
    "mediaSite" : "tweek"
    "date" : ISODate("2021-02-05T18:30:00.000+0000")
    "message" : "Hi Sohan I waiting near , please call me back"
}
]

Approach

Query: Create index using one field in mongo.

 
db.getCollection("message").find({$text:{$search:"RamChowk"}});

Output: 

[
    "_id" : ObjectId("60221f914cff3730adcdf8e1")
    "mediaSite" : "facebook"
    "date" : ISODate("2021-02-05T18:30:00.000+0000")
    "message" : "Hi Sohan I waiting near about RamChowk please call me back"
}
]


No comments:

Post a Comment