本文整理汇总了C#中BsonRegularExpression类的典型用法代码示例。如果您正苦于以下问题:C# BsonRegularExpression类的具体用法?C# BsonRegularExpression怎么用?C# BsonRegularExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BsonRegularExpression类属于命名空间,在下文中一共展示了BsonRegularExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Get
public LogSearchResponse Get(LogSearchRequest request)
{
request = request ?? new LogSearchRequest();
IFindFluent<BsonDocument, BsonDocument> cursor;
if (!request.IsEmpty)
{
List<FilterDefinition<BsonDocument>> queries = new List<FilterDefinition<BsonDocument>>();
if (!String.IsNullOrWhiteSpace(request.Query))
{
var queryExpr = new BsonRegularExpression(new Regex(request.Query, RegexOptions.IgnoreCase));
queries.Add(Builders< BsonDocument>.Filter.Or(
Builders< BsonDocument>.Filter.Regex(FieldNames.Message, queryExpr),
Builders<BsonDocument>.Filter.Regex(FieldNames.Loggername, queryExpr)
));
}
if (!String.IsNullOrWhiteSpace(request.Level))
{
var levels = request.Level.Split(',').Select(x => x.Trim()).ToArray();
queries.Add(Builders<BsonDocument>.Filter.In(FieldNames.Level, levels.Select(BsonValue.Create)));
}
cursor = Logs.Find(Builders<BsonDocument>.Filter.And(queries));
}
else
{
cursor = Logs.Find(Builders<BsonDocument>.Filter.Empty);
}
var response = new LogSearchResponse
{
Items = cursor
.Sort(Builders<BsonDocument>.Sort.Descending(FieldNames.Timestamp))
.Skip(request.LogsPerPage*(request.Page - 1))
.Limit(request.LogsPerPage)
.ToList()
.Select(x => x.ToDictionary()),
Count = cursor.Count()
};
return response;
}
示例2: TestAsRegexOptionS
public void TestAsRegexOptionS()
{
BsonValue v = new BsonRegularExpression("xyz", "s");
BsonValue s = "";
var r = v.AsRegex;
Assert.AreEqual(RegexOptions.Singleline, r.Options);
Assert.Throws<InvalidCastException>(() => { var x = s.AsRegex; });
}
示例3: FindAll
/// <summary>
/// finds all files
/// </summary>
/// <param name="startsWithPath"></param>
/// <param name="includeTempFiles"></param>
/// <param name="skip"></param>
/// <param name="limit"></param>
/// <returns></returns>
public List<IStorageFile> FindAll(string startsWithPath = null, bool includeTempFiles = false, int? skip = null, int? limit = null)
{
//all filepaths are lowercase and all starts with folder separator
if (!string.IsNullOrWhiteSpace(startsWithPath))
{
startsWithPath = startsWithPath.ToLowerInvariant();
if (!startsWithPath.StartsWith(FOLDER_SEPARATOR))
startsWithPath = FOLDER_SEPARATOR + startsWithPath;
}
List<IMongoQuery> queries = new List<IMongoQuery>();
if (!includeTempFiles)
{
IMongoQuery tmpFolderQuery = Query.Matches("filename", new BsonRegularExpression("^(/" + TMP_FOLDER_NAME + "/)", "i"));
IMongoQuery tmpFolderExclideQuery = Query.Not(tmpFolderQuery);
queries.Add(tmpFolderExclideQuery);
}
if (!string.IsNullOrWhiteSpace(startsWithPath))
{
//TODO change to starts with - now is contains
var regex = new BsonRegularExpression(string.Format(".*{0}.*", startsWithPath), "i");
queries.Add(Query.Matches("filename", regex));
}
IMongoQuery finalQuery = queries.Count > 0 ? Query.And(queries) : Query.Null;
var cursor = gfs.Find(finalQuery);
if (skip.HasValue)
cursor.SetSkip(skip.Value);
if (limit.HasValue)
cursor.SetLimit(limit.Value);
IEnumerable<MongoGridFSFileInfo> files = cursor.ToList();
return cursor.AsQueryable().Select(f => (IStorageFile)(new MongoStorageFile(f))).ToList();
}
示例4: Files
/// <summary>
/// Display all the files
/// </summary>
public ActionResult Files(string search)
{
var mongo = MongoServer.Create(MongoConnectionString);
var db = mongo.GetDatabase(MongoDatabase);
var gfs = db.GridFS;
var files = new List<MongoGridFSFileInfo>();
if (! string.IsNullOrEmpty(search)) {
var regex = new BsonRegularExpression(string.Format(".*{0}.*", search), "i");
files = gfs.Find(
Query.Or(
Query.Matches("metadata.title", regex),
Query.Matches("filename", regex)))
.ToList();
} else {
files = gfs.FindAll().ToList();
}
mongo.Disconnect();
return View(files);
}
示例5: GetRegularExpressionToken
private static JsonToken GetRegularExpressionToken(JsonBuffer buffer)
{
// opening slash has already been read
var start = buffer.Position - 1;
var state = RegularExpressionState.InPattern;
while (true)
{
var c = buffer.Read();
switch (state)
{
case RegularExpressionState.InPattern:
switch (c)
{
case '/': state = RegularExpressionState.InOptions; break;
case '\\': state = RegularExpressionState.InEscapeSequence; break;
default: state = RegularExpressionState.InPattern; break;
}
break;
case RegularExpressionState.InEscapeSequence:
state = RegularExpressionState.InPattern;
break;
case RegularExpressionState.InOptions:
switch (c)
{
case 'i':
case 'm':
case 'x':
case 's':
state = RegularExpressionState.InOptions;
break;
case ',':
case '}':
case ']':
case ')':
case -1:
state = RegularExpressionState.Done;
break;
default:
if (char.IsWhiteSpace((char)c))
{
state = RegularExpressionState.Done;
}
else
{
state = RegularExpressionState.Invalid;
}
break;
}
break;
}
switch (state)
{
case RegularExpressionState.Done:
buffer.UnRead(c);
var lexeme = buffer.Substring(start, buffer.Position - start);
var regex = new BsonRegularExpression(lexeme);
return new RegularExpressionJsonToken(lexeme, regex);
case RegularExpressionState.Invalid:
throw new FileFormatException(FormatMessage("Invalid JSON regular expression", buffer, start));
}
}
}
示例6: RegularExpressionJsonToken
// constructors
/// <summary>
/// Initializes a new instance of the RegularExpressionJsonToken class.
/// </summary>
/// <param name="lexeme">The lexeme.</param>
/// <param name="value">The BsonRegularExpression value.</param>
public RegularExpressionJsonToken(string lexeme, BsonRegularExpression value)
: base(JsonTokenType.RegularExpression, lexeme)
{
this.value = value;
}
示例7: GetUserByName
internal static kUser GetUserByName(string name)
{
var regex = new BsonRegularExpression(new Regex("^" + name + "$", RegexOptions.IgnoreCase));
var query = Query<kUser>.Matches(u => u.Name, regex);
return Db.GetCollection<kUser>("users").FindOne(query);
}
示例8: GetSession
private void GetSession(int sessionId)
{
// Get the database instance of mongo
var AuctionVehicles = ConnectToMongo();
// Get mongo Collection of documents, return them as a type of pre-made object (auction vehicle)
// param that is passed in is the string for the collection name (Vehicles)
var collection = AuctionVehicles.GetCollection<AuctionVehicle>("vehicles");
///
/// This searches for the searchsession 181
///
var query = new QueryDocument("_searchSession", 181);
///
/// This one gets all
var cursor = collection.FindAllAs<AuctionVehicle>();
///
/// This one gets one from using the above query.
///
// var cursor = collection.Find(query);
// Set returned cursor's sort params (sort by search session id decending).
cursor.SetSortOrder(SortBy.Descending("_searchSession"));
// Set limit of returned documents.
cursor.SetLimit(1);
// Convert the cursort to a list so can enumerate.
var list = cursor.ToList();
// Get the latest search session (first session in the list)
int latestSession = list[0]._searchSession;
// this one works for ANDS
var biggerQuery = Query.And(
Query.EQ("make", "TOYOTA"),
Query.EQ("model", "HIACE VAN")
);
// This one works for ORs
var queryOr = Query.Or(
Query.EQ("model", "SAFARI"),
Query.EQ("model", "ASTRO"),
Query.EQ("model", "CHEVYVAN")
);
// Create regex for mongo wildcard search
var regex = new BsonRegularExpression("/.*" + "USS Nagoya" + "*/");
// This one works for ands and regex
// This is my favourite way to query. each one is an And and finally a regex to do wildcard text search - remember the bsonregularexpression.
var biggerQueryDocumentStyle = new QueryDocument()
{
{"make", "TOYOTA"},
{"model", "HIACE VAN"},
{"htmlData.htmlRow", regex}
};
var cursor3 = collection.Find(biggerQueryDocumentStyle);
var cursor2 = collection.Find(biggerQuery);
var cursor3list = cursor3.ToList();
var cursor2list = cursor2.ToList();
/*
foreach (var item in cursor2list) {
Console.WriteLine(item.make + " " + item.model + " " + "Session ID = " + item._searchSession);
Console.ReadLine();
}
foreach (var item in cursor3list) {
Console.WriteLine(item.make + " " + item.model + " " + "Session ID = " + item._searchSession);
Console.ReadLine();
}
/*
*
foreach (var item in list)
{
Console.WriteLine(item.make + " " + item.model + " " + "Session ID = " + item._searchSession);
Console.ReadLine();
}
*/
}
示例9: TestBsonRegularExpression
public void TestBsonRegularExpression()
{
var value = new BsonRegularExpression("pattern", "imxs");
Assert.AreSame(value, ((IConvertible)value).ToType(typeof(object), null));
Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(value));
Assert.Throws<InvalidCastException>(() => Convert.ToByte(value));
Assert.Throws<InvalidCastException>(() => Convert.ToChar(value));
Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(value));
Assert.Throws<InvalidCastException>(() => Convert.ToDecimal(value));
Assert.Throws<InvalidCastException>(() => Convert.ToDouble(value));
Assert.Throws<InvalidCastException>(() => Convert.ToInt16(value));
Assert.Throws<InvalidCastException>(() => Convert.ToInt32(value));
Assert.Throws<InvalidCastException>(() => Convert.ToInt64(value));
Assert.Throws<InvalidCastException>(() => Convert.ToSByte(value));
Assert.Throws<InvalidCastException>(() => Convert.ToSingle(value));
Assert.Throws<InvalidCastException>(() => Convert.ToString(value));
Assert.Throws<InvalidCastException>(() => Convert.ToUInt16(value));
Assert.Throws<InvalidCastException>(() => Convert.ToUInt32(value));
Assert.Throws<InvalidCastException>(() => Convert.ToUInt64(value));
}
示例10: TestAsBsonRegularExpression
public void TestAsBsonRegularExpression()
{
BsonValue v = new BsonRegularExpression("pattern", "options");
BsonValue s = "";
var r = v.AsBsonRegularExpression;
Assert.AreEqual("pattern", r.Pattern);
Assert.AreEqual("options", r.Options);
Assert.Throws<InvalidCastException>(() => { var x = s.AsBsonRegularExpression; });
}
示例11: WriteRegularExpression
/// <summary>
/// Writes a BSON regular expression to the writer.
/// </summary>
/// <param name="regex">A BsonRegularExpression.</param>
public override void WriteRegularExpression(BsonRegularExpression regex)
{
if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
if (State != BsonWriterState.Value)
{
ThrowInvalidState("WriteRegularExpression", BsonWriterState.Value);
}
WriteValue(regex);
State = GetNextState();
}
示例12: TestClass
public TestClass(
BsonRegularExpression value
)
{
this.B = value;
this.V = value;
}
示例13: WriteRegularExpression_should_have_expected_result
public void WriteRegularExpression_should_have_expected_result()
{
var subject = CreateSubject();
var value = new BsonRegularExpression("abc", "i");
WriteNested(subject, () => subject.WriteRegularExpression(value));
AssertBsonEquals(subject, "{ x : /abc/i }");
}
示例14: TestBsonRegularExpressionConstructors
public void TestBsonRegularExpressionConstructors()
{
var regex = new BsonRegularExpression("pattern");
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("", regex.Options);
regex = new BsonRegularExpression("/pattern/i");
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("i", regex.Options);
regex = new BsonRegularExpression(@"/pattern\/withslash/i");
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern/withslash", regex.Pattern);
Assert.AreEqual("i", regex.Options);
regex = new BsonRegularExpression("pattern", "i");
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("i", regex.Options);
regex = new BsonRegularExpression(new Regex("pattern"));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("", regex.Options);
regex = new BsonRegularExpression(new Regex("pattern", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("imxs", regex.Options);
regex = new BsonRegularExpression(new Regex("pattern", RegexOptions.IgnoreCase));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("i", regex.Options);
regex = new BsonRegularExpression(new Regex("pattern", RegexOptions.Multiline));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("m", regex.Options);
regex = new BsonRegularExpression(new Regex("pattern", RegexOptions.IgnorePatternWhitespace));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("x", regex.Options);
regex = new BsonRegularExpression(new Regex("pattern", RegexOptions.Singleline));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("s", regex.Options);
}
示例15: TestMapBsonRegularExpression
public void TestMapBsonRegularExpression() {
var value = new BsonRegularExpression("pattern", "options");
var bsonValue = (BsonRegularExpression) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
var bsonRegularExpression = (BsonRegularExpression) BsonTypeMapper.MapToBsonValue(value, BsonType.RegularExpression);
Assert.AreSame(value, bsonRegularExpression);
}