Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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