Search code examples
mongodbmongodb-.net-driver

MongoDB query: how to check if a string stored in a document is contained in another string


I have a collection with 8k+ strings and I need to check if a particular string is contained in another string. For example:

StringInDb = "this is a string"
TheOtherString = "this is a long string that contains this is a string"

with linq I used something like:

from s in DB.Table 
where TheOtherString.IndexOf(s.StringInDb ) > -1
select s.StringInDb;

How can I do this (efficiently) in mongodb (even better using the c# .net driver)?


Solution

  • To me this sounds like you need to use map/reduce: map out all your strings from the DB and reduce to the ones contained in your long string. Cant remember the C# off the top of my head. Can find it later if you want.

    Update: The native language of MongoDB is JavaScript and Map/Reduce is run "inside the mongodb engine", which implies that the map and reduce function must be JavaScript, not C#. They can be called from C# though, as illustrated by this example taken from the official MogoDB C# driver documentation (http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-MapReducemethod). The example counts how many times each key is found in a collection:

    var map =
      "function() {" +
      "    for (var key in this) {" +
      "        emit(key, { count : 1 });" +
      "    }" +
      "}";
    
    var reduce =
      "function(key, emits) {" +
      "    total = 0;" +
      "    for (var i in emits) {" +
      "        total += emits[i].count;" +
      "    }" +
      "    return { count : total };" +
      "}";
    
    var mr = collection.MapReduce(map, reduce);
    foreach (var document in mr.GetResults()) {
      Console.WriteLine(document.ToJson());
    }