Mr Jones
Mr Jones

Reputation: 181

Searching for string pattern that matches string in array in MongoDB C#

I can't seem to get this working in MongoDB using the C# driver. I have spent a while on it now without any luck. I have an array of strings and I want to return all mongo docs that contains any of the words in the array.

I get the array string from a collection. And the array would contain items as such: ["moon", "cow", "Neil"]

I have a collections as such:

{_id: "xxxxx1", story:"The cow jump over the moon"}
{_id: "xxxxx2", story:"Neil Armstrong landed on the moon in the 1960s"}
{_id: "xxxxx3", story:"The moon is very bright tonight."}
{_id: "xxxxx4", story:"Itsy winchie spider climb up the spout. moon is cool."}
{_id: "xxxxx5", story:"moon"}
{_id: "xxxxx6", story:"no text match here mate"}

So from the array and collection, the first 5 documents should be released since they contain either "moon", "cow" or "Neil". The last document shouldn't be returned since it doesn't contain any of those words.

Below is my code that I am stuck on. It only returns document xxxx5 since it contains moon and nothing else. I am almost there, but not quite. Hope someone can help.

var userId = "12345";
var connectionString = ConfigurationManager.AppSettings["MongoDBConnectionString"];
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase(ConfigurationManager.AppSettings["MongoDBDatabase"]);

var fCollection = database.GetCollection<BsonDocument>("words");
var fQuery = Query.EQ("UserId", userId);
var fDoc = fCollection.FindAs<Words>(fQuery).SetFields(Fields.Exclude("_id"));
var list = fDoc.ToList();

var words = list.Select(t => t.Word).ToArray();

var collection = database.GetCollection<BsonDocument>("stories");
var query = Query.In("story", new BsonArray(words);


var doc =     collection.Find(query).SetSortOrder(SortBy.Descending("Submitted")).Skip(skip).Take(limit);

var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
return doc.ToJson(jsonWriterSettings);

Upvotes: 1

Views: 2549

Answers (2)

Kay
Kay

Reputation: 3012

Have you tried using the text index? it's an beta feature available in v2.4 that you can manually enable: http://docs.mongodb.org/manual/tutorial/enable-text-search/

See the following for info on the index itself: http://docs.mongodb.org/manual/core/index-text/

And the following page for examples of text search. http://docs.mongodb.org/manual/reference/command/text/#dbcmd.text

Upvotes: 0

i3arnon
i3arnon

Reputation: 116548

You can use Regex when you search a string in MongoDB:

Query.Matches("story","<Regex for: moon or cow or Neil>");

Look here to see how to write a regex that matches multiple words. It's basically this:

^(?=.*\bmoon\b)(?=.*\bcow\b)(?=.*\bNeil\b)

In conclusion:

collection.Find(Query.Matches(
    "story",
    "^(?=.*\bmoon\b)(?=.*\bcow\b)(?=.*\bNeil\b)"))
    .SetSortOrder(SortBy.Descending("Submitted")).Skip(skip).Take(limit);

Upvotes: 1

Related Questions