Last active
March 31, 2021 09:51
-
-
Save dj-nitehawk/b2cb3af45ae85557efdc7e1a99dde972 to your computer and use it in GitHub Desktop.
advanced query injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MongoDB.Entities; | |
using System; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace TestApplication | |
{ | |
public class UserActivity : Entity | |
{ | |
public string RawUrl { get; set; } | |
public string StartTime { get; set; } | |
public string EndTime { get; set; } | |
} | |
public class Result | |
{ | |
public double AvgPerDayMins { get; set; } | |
} | |
public static class Program | |
{ | |
private static async Task Main() | |
{ | |
await DB.InitAsync("test"); | |
var searchWord = "edit"; | |
var query = new Template<UserActivity, Result>(@" | |
[ | |
{ | |
$match: { | |
RawUrl: <search_word>, | |
EndTime: { $ne: null } | |
} | |
}, | |
{ | |
$set: { | |
EndTime: { $toDate: '$EndTime' }, | |
StartTime: { $toDate: '$StartTime' } | |
} | |
}, | |
{ | |
$set: { | |
Duration: { $divide: [{ $subtract: ['$EndTime', '$StartTime'] }, 60000] } | |
} | |
}, | |
{ | |
$group: { | |
_id: null, | |
Total: { $sum: '$Duration' }, | |
MinDate: { $min: '$StartTime' }, | |
MaxDate: { $max: '$StartTime' } | |
} | |
}, | |
{ | |
$set: { | |
TotalDays: { | |
$add: [ | |
{ | |
$round: [ | |
{ | |
$divide: [ | |
{ $subtract: ['$MaxDate', '$MinDate'] }, 86400000] | |
}, 0] | |
}, 1] | |
} | |
} | |
}, | |
{ | |
$project: { | |
_id: 0, | |
AvgPerDayMins: { $divide: ['$Total', '$TotalDays'] } | |
} | |
} | |
]") | |
.Tag("search_word", $"/{searchWord}/i"); | |
var result = await DB.PipelineAsync(query); | |
Console.WriteLine("Average Per Day Minutes: " + result[0].AvgPerDayMins); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment