Let say I have a user like bellow
[
{
username : "madhusudhanarao.ch"
}
]
I create index text for username field, but I can't use text search to find this user
db.collection.find({$text : {$search : "madhus" }})
I have to search the full text like madhusudhanarao or ch
I know I can use regex to search but all my code are use $text $search. I can't fix all of them.
As we normally use in sql, the "%like", We use regex for the same purpose on MongoDB
Better read documentation, and you can find Option which you can implement on your search.
db.colection.find({username: {$regex : text }})
Related
I've problem to find the good document search request do find all documents containing in their 'name' field all the specified values.
I've this document:
{
"_id" : ObjectId("607c1caa4b2964d0185301ff"),
"nb" : 1,
"name" : "mini computer 24GB"
}
When I run the following find request...
db.getCollection('test').find({"$text":{$search:'computer dummy'}})
... the document is returned. An OR is done but I want a AND operation. Should I use a list of $and ?
Many thanks
Ok I found. each word must be in quotes as this:
db.getCollection('test').find({$text:{$search:"\"computer\" \"24GB\""}})
Not really intuitive!
what is wrong with this query ?
db.collection.find( { "name" : "/^test$/i", "group" : "/^Default$/i"} )
I am trying to find an object with name=test, group=default, but not case sensitive.
but I am not getting the result although I know I have this document in the database:
I used exactly as in mongo website it's explained:
In MongoDB, you can also use regular expression objects (i.e. /pattern/) to specify regular expressions:
{ <field>: /pattern/<options> }
The query in its essence is right, you just have a minor syntax error.
In javascript (Which Mongo shell is based on) a regex is of the form of /xxx/ and not "/xxx/", the ladder being a string expression.
So just change your query into this:
db.collection.find( { "name" : /^test$/i, "group" : /^Default$/i} )
I am trying to edit the data in my mongodb collection after importing the data from a csv file. One of my fields contains a mixture of alphabetic and numeric characters, and I want to edit it such that it only contains alphabetic characters to make querying easier
So I want to change this:
"categories" : [
"9100005:1:factual",
"9200041:2:arts_culture_and_the_media",
"9200055:2:history"
]
Into this:
"categories" : [
"factual",
"arts culture and the media",
"history"
]
I know that using $regex I can query for certain categories, so using:
db.bbc.find( { categories: {$regex: /factual/ }} )
I get all records with "factual" as a category, but I am unsure how to use $regex to query for multiple categories and I just feel avoiding using regular expressions entirely would be easier. Does anyone know what command I would have to run to be able to do this? Thanks.
Try this
db.test1.find().snapshot().forEach(function (el) {
for(a in el.Category){
print(el.Category[a].replace(/[^A-Za-z]/g, ""));
el.Category[a]=el.Category[a].replace(/[^A-Za-z]/g, "");
}
db.test1.save(el);
});
Edit:
To Preserve Space Just add space in regex.
You can use this line
el.Category[a].replace(/[^A-Za-z]*$/g, "");
To Handle Underscore
el.Category[a].replace(/[^A-Za-z_]*$/g, "");
I'm trying to implement full text search in my Mongo database. It's a database of audio tracks metadata. I wan't to search by artistName and title of a track. I have these records in the tracks collection (showing only important fields):
db.tracks.find({},{artistName: 1, title: 1})
{ "_id" : "A10328E00047516670", "artistName" : "Tapani Kansa", "title" : "Tuulia" }
{ "_id" : "A10328E00047516661", "artistName" : "Tapani Kansa", "title" : "Rakkautemme valssi" }
{ "_id" : "A10328E0004751669W", "artistName" : "Tapani Kansa", "title" : "Täysikuu" }
{ "_id" : "A10328E0004751668Y", "artistName" : "Tapani Kansa", "title" : "Muista minua" }
I've created the text index on this collection:
db.tracks.createIndex({artistName: 'text', title: 'text', lyrics: 'text'})
But when I try to search the tracks, no results are returned:
rs-ds047345:PRIMARY> db.tracks.find({$text: {$search: 'Tapani'}}).size()
0
rs-ds047345:PRIMARY> db.tracks.find({$text: {$search: 'Rakkautemme valssi'}}).size()
0
I accidentally noticed, that when I crop some letters from the end of the searched word, I'm starting to get some results... so full text search somehow works, just not in way I would like and expect.
db.tracks.find({$text: {$search: 'Tapa'}}).size()
12
rs-ds047345:PRIMARY> db.tracks.find({$text: {$search: 'Rakkaute'}}).size()
1
Could someone please tell me, how can I search the database using full words, or what I'm doing wrong?
I've tried that on MongoDB versions 3.0.8 and 3.2.1
according to spec -
For case insensitive and diacritic insensitive text searches, the
$text operator matches on the complete stemmed word. So if a document
field contains the word blueberry, a search on the term blue will not
match. However, blueberry or blueberries will match.
what I will suggest is normal index and a regex search
db.tracks.createIndex({"artistName": 1})
db.tracks.createIndex({ "title" : 1})
db.tracks.createIndex({ "lyrics": 1})
db.tracks.find({artistName:"/Tap/[0-10]"}).explain()
the square bracket will force index scan for regex instead of colscan
was testing on 3.0.6 and 3.2.3 with no luck :(
So, the problem was in the documents stored in database. I didn't noticed that they contains a field named language, which changes full text search behaviour, although I tried to disable word stemming by by setting language: 'none' in index and queries.
When I renamed the language field to a different name, the full text search started to work exactly as I expect.
We have an application where solar-net is integrated with mongodb for searching and full text search is working fine. Now we have to change full text search to exact word based search for example if "DELL" is entered in Search input field, it should only bring up results that are "DELL", and not "DELL Inspiron". Please let us know how to change full text search to exact word based search.Is there any regular expression to do this. Search is based on multiple fields. Please help me.
Thanks
Tarlok
You can use regex as following :
To Search all names starts with "DELL"
db.collectionName.find( { "name" : { $regex : "^DELL.*", $options : "i"} } )
To Search all names contains "DELL" :
db.collectionName.find( { "name" : { $regex : "DELL.*", $options : "i"} } )
here $options : "i" defines ignore case
for more detail visit This.