Exact word based search in Solarnet & Mongodb - mongodb

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.

Related

MongoDB Text search - search contain in string

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 }})

Find doc from field having all specified words

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!

How to remove all entries that contain a value in a certain field? | MONGODB

This is the structure I am working with:
{ "_id" : ObjectId("58dabf31fbd927071b51dhrc8"), "email" : "redacted",
"file_name" : "registration" }
I need to be able to remove all entries in the file_name field that contain "registration" -- does anyone have any idea how I can achieve this?
You can delete by regex matching the value.
{"file_name":{$regex:/registration/}}
See mongodb regex for me info
In your case
db.collection.deleteMany({"file_name":{$regex:/(.*)?registration(.*)?/}});

Mongo full text search doesn't find

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.

Mongo query with regex fails when backslash\newline are present

I'm using MongoDB with Sails.
db.post.find( { body: {"$regex" : /^.*twitter.*$/i }}).
This query is supposed to find only posts which contain 'twitter' in their body-field.
For some reason it won't find a match if a backslash is present in the field (like a newline).
Is this known? What can I do about it?
Here are two examples to showcase the problem:
This document is returned by the find-command above:
{
"_id" : ObjectId("54e0d7eac2280519ac14cfda"),
"title" : "Cool Post",
"body" : "Twitter Twitter twitter twittor"
}
This document is not returned by the find-command above:
{
"_id" : ObjectId("54e0d7eac2280519ac14cfdb"),
"title" : "Cool Post with Linebreaks",
"body" : "This is a cool post with Twitter mentioned into it \n Foo bar"
}
Is this known?
No. Its not an issue with the MongoDb search Engine.
What can I do about it?
You could change your Regular Expression to:
var regex = new RegExp("twitter","i");
db.posts.find( { body: regex});
The problem with your code is the decimal point .
According to the doc,
.
(The decimal point) matches any single character except the newline
character.
which is why the string with a newline character is not taken to be a match.