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