Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Created July 3, 2020 17:53
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/bd65e6e663847a19a3b07b46e540dd4d to your computer and use it in GitHub Desktop.
Save dj-nitehawk/bd65e6e663847a19a3b07b46e540dd4d to your computer and use it in GitHub Desktop.
deeply nested shitstorm ;-)
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Entities;
using MongoDB.Entities.Core;
using System.Linq;
namespace TestApp
{
public class Team : Entity
{
public Dashboard[] Dashboards { get; set; }
}
public class Dashboard
{
public Version[] Versions { get; set; }
}
public class Version
{
public Widget[] Widgets { get; set; }
}
public class Widget
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string ID { get; set; }
public string WidgetName { get; set; }
}
internal static class Program
{
private static void Main()
{
new DB("test");
var widgetID = ObjectId.GenerateNewId().ToString();
new Team
{
Dashboards = new[] {
new Dashboard {
Versions = new[] {
new Version {
Widgets = new[] {
new Widget {
ID = widgetID,
WidgetName = "test widget" } } } } } }
}.Save();
var widget = DB.Fluent<Team>()
.Match(f => f.Eq("Dashboards.Versions.Widgets._id", ObjectId.Parse(widgetID)))
.Unwind("Dashboards")
.Unwind("Dashboards.Versions")
.Unwind("Dashboards.Versions.Widgets")
.ReplaceWith<Widget>("$Dashboards.Versions.Widgets")
.Match(w => w.ID == widgetID)
.SingleOrDefault();
DB.Update<Team>()
.Match(f => f.Eq("Dashboards.Versions.Widgets._id", ObjectId.Parse(widgetID)))
.WithArrayFilter("{ 'x._id' : { $eq : ObjectId('" + widgetID + "') } }")
.Modify("{ $set : { 'Dashboards.$[].Versions.$[].Widgets.$[x].WidgetName' : 'updated widget name' } }")
.Execute();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment