Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Last active March 9, 2020 09:46
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/15279dbfeaa8807ff48f75a4e28b9b62 to your computer and use it in GitHub Desktop.
Save dj-nitehawk/15279dbfeaa8807ff48f75a4e28b9b62 to your computer and use it in GitHub Desktop.
string template example - lookup pipeline
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