1

我有以下课程:

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

如何查询标签?我想用like %term%. 我试过了/term/i,但它不起作用。

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

有任何想法吗?

4

3 回答 3

2

正确的查询是:

 var tags = db.GetCollection().Find( 
 new { Tags = Q.Matches(string.Format(@".*{0}.*", term))},
 new { Tags = OrderBy.Descending }, new { Tags = 1}, 8, 0).ToList();
于 2011-05-27T13:23:54.980 回答
0

看看下面的问题。

如何使用官方 C# 驱动程序在 mongoDB 中执行 SQL Like 运算符

于 2011-05-26T17:52:20.743 回答
0

更好的是,您可以在 NORM LINQ 提供程序中使用正则表达式(参考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();
于 2011-08-29T18:13:05.203 回答