Mongodb C# 驱动程序执行字符串包含对嵌入文档中 属性 的查询
Mongodb C# driver Perform string contains query on a property in an embedded document
我有以下简化模型:
public class Entity
{
public Guid Id { get; set; }
public IList<ComponentProperty> Components { get; private set; }
}
public class ComponentProperty
{
public string PropertyName { get; set; }
public string Value { get; set; }
}
我希望能够找到其 属性 值字符串包含搜索字符串的任何实体,为此我根据建议 here 编写了以下查询,它隐含地使用了正则表达式:
var query = _context.GetCollection<Entity>()
.AsQueryable()
.Where(t => t.Components.Any(c => c.Value.ToLower() == queryOptions.Filter));
此查询生成以下 json 和(错误地)returns 0 行:
aggregate([{ "$match" : { "Components" : { "$elemMatch" : { "Value" : /^'green'$/i} } } }])
但是,生成正确结果的手工查询如下:
aggregate([{ "$match" : { "Components" : { "$elemMatch" : { "Value" : {$regex: '.*green.*' } } } } }])
也许,我在使用 c# 驱动程序的方法中忽略了一些东西,我们将不胜感激。
将您的 where
子句更改为:
.Where(e => e.Components.Any(c => c.Value.ToLower().Contains(queryOptions.Filter)))
生成此聚合:
db.Entity.aggregate([
{
"$match": {
"Components.Value": /green/is
}
}
])
这是一个测试程序:
using MongoDB.Entities;
using MongoDB.Entities.Core;
using System.Collections.Generic;
using System.Linq;
namespace Whosebug
{
public class Ntity : Entity
{
public IList<ComponentProperty> Components { get; set; }
}
public class ComponentProperty
{
public string PropertyName { get; set; }
public string Value { get; set; }
}
public static class Program
{
private static void Main()
{
new DB("test");
new Ntity
{
Components = new List<ComponentProperty> {
new ComponentProperty {
PropertyName = "test",
Value = "the RED color" }
}
}.Save();
new Ntity
{
Components = new List<ComponentProperty> {
new ComponentProperty {
PropertyName = "test",
Value = "the Green color" }
}
}.Save();
var result = DB.Queryable<Ntity>()
.Where(e => e.Components.Any(c => c.Value.ToLower().Contains("green")))
.ToList();
}
}
}
我有以下简化模型:
public class Entity
{
public Guid Id { get; set; }
public IList<ComponentProperty> Components { get; private set; }
}
public class ComponentProperty
{
public string PropertyName { get; set; }
public string Value { get; set; }
}
我希望能够找到其 属性 值字符串包含搜索字符串的任何实体,为此我根据建议 here 编写了以下查询,它隐含地使用了正则表达式:
var query = _context.GetCollection<Entity>()
.AsQueryable()
.Where(t => t.Components.Any(c => c.Value.ToLower() == queryOptions.Filter));
此查询生成以下 json 和(错误地)returns 0 行:
aggregate([{ "$match" : { "Components" : { "$elemMatch" : { "Value" : /^'green'$/i} } } }])
但是,生成正确结果的手工查询如下:
aggregate([{ "$match" : { "Components" : { "$elemMatch" : { "Value" : {$regex: '.*green.*' } } } } }])
也许,我在使用 c# 驱动程序的方法中忽略了一些东西,我们将不胜感激。
将您的 where
子句更改为:
.Where(e => e.Components.Any(c => c.Value.ToLower().Contains(queryOptions.Filter)))
生成此聚合:
db.Entity.aggregate([
{
"$match": {
"Components.Value": /green/is
}
}
])
这是一个测试程序:
using MongoDB.Entities;
using MongoDB.Entities.Core;
using System.Collections.Generic;
using System.Linq;
namespace Whosebug
{
public class Ntity : Entity
{
public IList<ComponentProperty> Components { get; set; }
}
public class ComponentProperty
{
public string PropertyName { get; set; }
public string Value { get; set; }
}
public static class Program
{
private static void Main()
{
new DB("test");
new Ntity
{
Components = new List<ComponentProperty> {
new ComponentProperty {
PropertyName = "test",
Value = "the RED color" }
}
}.Save();
new Ntity
{
Components = new List<ComponentProperty> {
new ComponentProperty {
PropertyName = "test",
Value = "the Green color" }
}
}.Save();
var result = DB.Queryable<Ntity>()
.Where(e => e.Components.Any(c => c.Value.ToLower().Contains("green")))
.ToList();
}
}
}