Skip to main content

MongoDB Exact Word Search Without $text

We can use simple $regex to find out documents with specific field have exact word in string value

For Example

Let's say we have following documents in collection sampledata


    _id : ObjectId("583bbccc592818101fb7f3f8"),
   "textdata" : "This is sample text data",
},

    _id : ObjectId("583bbccc592818101fb7e43w"),
   "textdata" : "data use by default",
},

    _id : ObjectId("583bbccc592818101fb723ed"),
   "textdata" : "bird can fly, This is not always true",
}

1 - Now we want only document which have "textdata" contains word "data"

db.sampledata.find({ "textdata" : { $regex : /\bdata\b/ })

Above query will return following documents


    _id : ObjectId("583bbccc592818101fb7f3f8"),
   "textdata" : "This is sample text data",
},

    _id : ObjectId("583bbccc592818101fb7e43w"),
   "textdata" : "data use by default",
}

2 - Now want only document which have "textdata" contains word "use"  

db.sampledata.find({ "textdata" : { $regex : /\buse\b/ })

Above query will return following documents


    _id : ObjectId("583bbccc592818101fb7e43w"),
   "textdata" : "data use by default",
}

This will be helpful when your string data contains lots of words and you don't want to create $text index to minimize ram usages. For better performance use this trick with some index fields in where clause to narrow down document examine count.

To implement full text search in MongoDB use $text, You can search documents by word using $text when fields have text index (MongoDB official document on that)

Popular posts from this blog

SqlConnection from ObjectContext.Connection

Following is C# code to get the  SqlConnection from ObjectContext.Connection . This is usefully to run dynamically created query using ADO.NET using connection string specified into the EDMX ObjectContext object. C# Code Example : using ( EntityContext context = new EntityContext ()) { EntityConnection ec = ( EntityConnection )context.Connection; SqlConnection sc = ( SqlConnection )ec.StoreConnection; SqlDataAdapter da = new SqlDataAdapter (strQuery, sc); DataSet ds = new DataSet (); da.Fill(ds); return ds.Tables[0]; }

LINQ union with group by

In my recent development activity with LINQ query, I need to get the some of user points from tow different tables where user's points are based on some activities. There are many users who are into one table but not in another table and some are in both table. For example tables having data like this Table 1 UserID   Points 1           10 2           20 Table 2 UserID    Points 2           20 3           30 Result should be UserID    Points 1           10 2           40 3           30 I try to get this result with LINQ query using joins and group by samples from the web. I had googling to get something useful but dont get any success. So I try with new logic and here it is, I think it will help full to other also. Example Item class to use public cla...

Google Analytic Dashboard Controls

"Google Analytic Dashboard Controls" is costume controls library which contain ASP.NET costume controls which generate different types of charts like Google analytic dashboard using Google analytic data export api. Its include following controls. Visitors overviews Word map overview Traffic source overview Content overview Followings are the images of the resulting graphs using them Visitors Overview Traffic Source Overview Word Map Overview Content Visits Overview To use this controls into your ASP.NET projects follow the below steps. 1. Download "Google Analytics Desbord Controls.dll" 2. Add reference of "Google Analytics Desbord Controls.dll" to your project. 3. In toolbox add controls by Right Click -> Choose Items -> browse dll. 4. Now Drag and drop control to your .aspx file. 5. Set Properties GAEmailAddress to your Google Analytic account email id, GAPassword to password , GAProfileId ...