Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB查询:如何检查文档中存储的字符串是否包含在另一个字符串中_Mongodb_Mongodb .net Driver - Fatal编程技术网

MongoDB查询:如何检查文档中存储的字符串是否包含在另一个字符串中

MongoDB查询:如何检查文档中存储的字符串是否包含在另一个字符串中,mongodb,mongodb-.net-driver,Mongodb,Mongodb .net Driver,我有一个8k+字符串的集合,我需要检查一个特定字符串是否包含在另一个字符串中。例如: StringInDb = "this is a string" TheOtherString = "this is a long string that contains this is a string" 对于linq,我使用了如下内容: from s in DB.Table where TheOtherString.IndexOf(s.StringInDb ) > -1 select s.Stri

我有一个8k+字符串的集合,我需要检查一个特定字符串是否包含在另一个字符串中。例如:

StringInDb = "this is a string"
TheOtherString = "this is a long string that contains this is a string"
对于linq,我使用了如下内容:

from s in DB.Table 
where TheOtherString.IndexOf(s.StringInDb ) > -1
select s.StringInDb;

如何在mongodb中(更有效地使用c#.net驱动程序)执行此操作?

在mongodb中,因为
包含
您需要使用regexp,因此c#查询将如下所示:

var query = Query.Matches("StringParamName", 
     BsonRegularExpression.Create(".*this is a string.*", "-i"));
完成查询构建后,将此查询放入
Collection.FindAs(query)
方法

-i
-表示忽略大小写


mongodb中的Regexp工作缓慢,因为它不能使用索引。但是对于8k集合,它应该工作得非常快。

在mongodb中,对于
包含的
您需要使用regexp,因此c#查询将如下所示:

var query = Query.Matches("StringParamName", 
     BsonRegularExpression.Create(".*this is a string.*", "-i"));
完成查询构建后,将此查询放入
Collection.FindAs(query)
方法

-i
-表示忽略大小写


mongodb中的Regexp工作缓慢,因为它不能使用索引。但是对于8k集合,它应该工作得很快。

对我来说,这听起来像是需要使用map/reduce:从DB中映射出所有字符串,并将其还原为长字符串中包含的字符串。我记不起头顶上的字母了。如果你愿意的话,我可以稍后再找

更新:MongoDB的母语是JavaScript,Map/Reduce在“MongoDB引擎内部”运行,这意味着Map和Reduce函数必须是JavaScript,而不是C#。但是,它们可以从C#调用,正如这个来自官方MogoDB C#驱动程序文档的示例所示(http://www.mongodb.org/display/DOCS/CSharp+驱动程序+教程#CSharpDriver虚拟地图还原方法)。该示例统计在集合中找到每个键的次数:

var map =
  "function() {" +
  "    for (var key in this) {" +
  "        emit(key, { count : 1 });" +
  "    }" +
  "}";

var reduce =
  "function(key, emits) {" +
  "    total = 0;" +
  "    for (var i in emits) {" +
  "        total += emits[i].count;" +
  "    }" +
  "    return { count : total };" +
  "}";

var mr = collection.MapReduce(map, reduce);
foreach (var document in mr.GetResults()) {
  Console.WriteLine(document.ToJson());
}

对我来说,这听起来像是需要使用map/reduce:从数据库中映射出所有字符串,并将其还原为长字符串中包含的字符串。我记不起头顶上的字母了。如果你愿意的话,我可以稍后再找

更新:MongoDB的母语是JavaScript,Map/Reduce在“MongoDB引擎内部”运行,这意味着Map和Reduce函数必须是JavaScript,而不是C#。但是,它们可以从C#调用,正如这个来自官方MogoDB C#驱动程序文档的示例所示(http://www.mongodb.org/display/DOCS/CSharp+驱动程序+教程#CSharpDriver虚拟地图还原方法)。该示例统计在集合中找到每个键的次数:

var map =
  "function() {" +
  "    for (var key in this) {" +
  "        emit(key, { count : 1 });" +
  "    }" +
  "}";

var reduce =
  "function(key, emits) {" +
  "    total = 0;" +
  "    for (var i in emits) {" +
  "        total += emits[i].count;" +
  "    }" +
  "    return { count : total };" +
  "}";

var mr = collection.MapReduce(map, reduce);
foreach (var document in mr.GetResults()) {
  Console.WriteLine(document.ToJson());
}

这是我的生产系统中使用的包装器。当您应该始终调用GetBsonValue()时,它将为您完成其余的工作

/// <summary>
/// Makes a Bson object for current value object
/// </summary>
/// <returns>The Bson object for current value object</returns>
private BsonValue GetBsonValue()
{
    if (!_value.Contains(_wildCard))
        return _value;
    string pattern = ApplyWildCard();
    return BsonRegularExpression.Create(new Regex(pattern, RegexOptions.IgnoreCase));
}

/// <summary>
/// Finds wildcard characters and substitutes them in the value  string
/// </summary>
/// <returns></returns>
private string ApplyWildCard()
{
    return string.Format("^{0}$", _value.Replace(_wildCard, ".*"));
}

这是我的生产系统中使用的包装器。当您应该始终调用GetBsonValue()时,它将为您完成其余的工作

/// <summary>
/// Makes a Bson object for current value object
/// </summary>
/// <returns>The Bson object for current value object</returns>
private BsonValue GetBsonValue()
{
    if (!_value.Contains(_wildCard))
        return _value;
    string pattern = ApplyWildCard();
    return BsonRegularExpression.Create(new Regex(pattern, RegexOptions.IgnoreCase));
}

/// <summary>
/// Finds wildcard characters and substitutes them in the value  string
/// </summary>
/// <returns></returns>
private string ApplyWildCard()
{
    return string.Format("^{0}$", _value.Replace(_wildCard, ".*"));
}
这是一个长字符串,其中包含一个字符串\“。匹配(This.YourFieldName)”

这是你想要的吗

这是一个长字符串,其中包含一个字符串\“。匹配(This.YourFieldName)”


这是你想要的吗?

这是另一种方式。我需要检查其他字符串是否包含数据库中的任何字符串(StringInDb)。如果我使用您的方法,查询将为:var query=query.Matches(“StringParamName(这是一个字符串)”,BsonRegularExpression.Create(“这是一个包含这是一个字符串的长字符串”,“-I”);没有对手,情况正好相反。我需要检查其他字符串是否包含数据库中的任何字符串(StringInDb)。如果我使用您的方法,查询将为:var query=query.Matches(“StringParamName(这是一个字符串)”,BsonRegularExpression.Create(“这是一个包含这是一个字符串的长字符串”,“-I”);没有匹配我不明白这对我的问题有什么帮助,我遗漏了什么吗?如果你在某个字段中寻找“某个字符串”,那么你应该在搜索的值之前和之后用通配符字符装饰它是的,这很清楚,但正如我对Andrew所说的,我需要的是另一种方式。根据Christian的建议使用map/reduce解决。好的,你有这两种方法的一些基准吗(只是想知道)?我不明白这对我的问题有什么帮助,我遗漏了什么吗?如果你在寻找“一些字符串”在某个字段中,您应该在搜索的值之前和之后使用通配符字符对其进行修饰是的,这很清楚,但正如Andrew所说,我需要的是另一种方式。根据Christian的建议使用map/reduce解决。好的,你有这两种方法的一些基准吗(只是想知道)?谢谢,我会研究map/reduce。我使用mongodb才3天,对它一点也不熟悉。是的,如果你能找到C,那就太好了谢谢Christian,我遵照你的建议,用map/reduce完成了!你好我希望实现同样的目标。你能分享一下你的解决方案吗?我就是不能让它工作。。。谢谢谢谢,我会查一下地图/地图。我使用mongodb才3天,对它一点也不熟悉。是的,如果你能找到C,那就太好了谢谢Christian,我遵照你的建议,用map/reduce完成了!你好我希望实现同样的目标。你能分享一下你的解决方案吗?我就是不能让它工作。。。谢谢