
Untitled
By: a guest on
May 28th, 2012 | syntax:
None | size: 1.51 KB | hits: 14 | expires: Never
MongoDB query: how to check if a string stored in a document is contained in another string
StringInDb = "this is a string"
TheOtherString = "this is a long string that contains this is a string"
from s in DB.Table
where TheOtherString.IndexOf(s.StringInDb ) > -1
select s.StringInDb;
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());
}
var query = Query.Matches("StringParamName",
BsonRegularExpression.Create("*this is a string*", "-i"));
/// <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, ".*"));
}
public QueryComplete BuildQuery()
{
return Query.EQ(_key, GetBsonValue());
}