MongoDBDriver中通过过滤器实现文档查询 [数据库教程]
一、举例:在物流币点击记录里面查询出 同一IP,同一条线路,同一页面,四小时内点击次数
思路:1、根据当前查询的时间将时间回退到四小时前,然后将四小时前的时间转换为时间戳,然后组合其它条件查询 出大于对应时间戳的所有记录数。
string TheTimeStamp = TimeHelper.GetTimeStamp(requestTime.AddHours(-4));
2、构建查询过滤器
var filterBuilder = Builders<PageColllection>.Filter;var filter = filterBuilder.Eq(p => p.PXID, wid) & filterBuilder.Eq(p => p.ip, ip) & filterBuilder.Gte(p => p.unixTime, unixtemp) & filterBuilder.Eq(p => p.state, 1) & filterBuilder.Eq(p => p.URL, url);
方法如下所示:
publiclong AntiEvilclickRuleOne(string ip, int wid, DateTime requestTime, string url){
try{
long result = 0;long unixtemp = 0;string TheTimeStamp = TimeHelper.GetTimeStamp(requestTime.AddHours(-4));if (!long.TryParse(TheTimeStamp, out unixtemp)){
return0;}
var filterBuilder = Builders<PageColllection>.Filter;var filter = filterBuilder.Eq(p => p.PXID, wid) & filterBuilder.Eq(p => p.ip, ip) & filterBuilder.Gte(p => p.unixTime, unixtemp) & filterBuilder.Eq(p => p.state, 1) & filterBuilder.Eq(p => p.URL, url);result
= MongoDbHelper.GetDb().GetCollection<PageColllection>("PageColllection").Find(filter).CountDocuments();return result;}
catch (Exception ex){
AddLog.addLog_db(
"PageColllectionBLL.CheckRecordsByMemberIDLineID异常", ex.Message);return0;}
}
MongoDB Driver 中通过过滤器实现文档查询
以上是 MongoDBDriver中通过过滤器实现文档查询 [数据库教程] 的全部内容, 来源链接: utcz.com/z/535324.html