elranu
elranu

Reputation: 2312

MongoDB text search on string array

I have the following class:

public class Movie
{
    string Name get; set;
    string Director get;  set;
    IList<String> Tags get; set;
}

How I can query the Tags? I want to query as SQL with a like %term%. I tried with /term/i, but it does not work.

term = string.Format(@"/{0}/i", term);
var tags = db.GetCollection().Find( 
    new { Tags = term  }, 
    new { Tags = OrderBy.Descending },
    new { Tags = 1}, 8, 0).ToList();

Any ideas?

Upvotes: 1

Views: 1655

Answers (3)

Zaid Masud
Zaid Masud

Reputation: 13443

Better still, you can use a Regex in the NORM LINQ provider (reference http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions).

_provider.GetCollection<T>().AsQueryable()
  .Where(t =>
    Regex.IsMatch(t.Tags, String.Format(".*{0}.*", term), RegexOptions.IgnoreCase))
 .OrderByDescending();

Upvotes: 0

elranu
elranu

Reputation: 2312

The correct query is:

 var tags = db.GetCollection().Find( 
 new { Tags = Q.Matches(string.Format(@".*{0}.*", term))},
 new { Tags = OrderBy.Descending }, new { Tags = 1}, 8, 0).ToList();

Upvotes: 2

atbebtg
atbebtg

Reputation: 4083

Take a look at the following question.

How do you do SQL Like operator in mongoDB using the official C# driver

Upvotes: 0

Related Questions