Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Last active March 31, 2021 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj-nitehawk/b2cb3af45ae85557efdc7e1a99dde972 to your computer and use it in GitHub Desktop.
Save dj-nitehawk/b2cb3af45ae85557efdc7e1a99dde972 to your computer and use it in GitHub Desktop.
advanced query injection
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