1

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

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please post some code that would make the search case insensitive?
@CSLewis take a look at this: stackoverflow.com/questions/4458950/…
0

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

1 Comment

In the mongo shell, you could do db.words.find( { story: /cow|land|moon/ } ), but not sure how to construct the regex in C# -- perhaps BsonRegularExpression.Create(new Regex("cow|land|moon"))?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.