Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk

dj-nitehawk/Program.cs

Last active Mar 31, 2021
Embed
What would you like to do?
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