Last active
March 9, 2020 09:46
-
-
Save dj-nitehawk/15279dbfeaa8807ff48f75a4e28b9b62 to your computer and use it in GitHub Desktop.
string template example - lookup pipeline
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.Bson; | |
using MongoDB.Bson.Serialization.Attributes; | |
using MongoDB.Driver; | |
using MongoDB.Entities; | |
using MongoDB.Entities.Core; | |
using System.Linq; | |
namespace StackOverFlow | |
{ | |
public class Channel : Entity | |
{ | |
public string Name { get; set; } | |
public Episode[] Episodes { get; set; } | |
} | |
public class Episode | |
{ | |
public string Name { get; set; } | |
[BsonRepresentation(BsonType.ObjectId)] | |
public string[] Tracks { get; set; } | |
} | |
public class Track : Entity | |
{ | |
public string Language { get; set; } | |
} | |
public class EpisodeWithTracks | |
{ | |
public string Name { get; set; } | |
public Track[] Tracks { get; set; } | |
} | |
public static class Program | |
{ | |
private static void Main() | |
{ | |
new DB("test"); | |
var trk1 = new Track { Language = "english" }; | |
var trk2 = new Track { Language = "spanish" }; | |
new[] { trk1, trk2 }.Save(); | |
new Channel | |
{ | |
Name = "channel one", | |
Episodes = new[] { | |
new Episode{ | |
Name = "first episode", | |
Tracks = new[]{ trk1.ID, trk2.ID} } | |
} | |
}.Save(); | |
var pipeline = new Template<Channel, EpisodeWithTracks>(@" | |
[ | |
{ | |
'$unwind': '$<Episodes>' | |
}, | |
{ | |
'$lookup': { | |
'from': '<track_collection>', | |
'localField': '<Episodes.Tracks>', | |
'foreignField': '_id', | |
'as': '<Tracks>' | |
} | |
} | |
]") | |
.Path(c => c.Episodes) | |
.Tag("track_collection", trk1.CollectionName()) | |
.Path(c => c.Episodes[0].Tracks) | |
.PathOfResult(ewt => ewt.Tracks); | |
var result = DB.Aggregate(pipeline) | |
.ToList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment