I'm pretty new to MongoDB and already I face a sweaty challenge. I'm trying to get an element from an array with objects inside of an array with objects (if that makes sense). This is how a document looks like and what I am trying to get:
So basically the structure is as follows:
- OptionMenus
- OptionMenuSubject
- OptionMenuItem
- OptionMenuSubject
To get the OptionMenuItem I matched to pull it off using the aggregation pipeline tool from MongoDB Compass tool
[{
$unwind: {
path: '$subjects'
}
}, {
$unwind: {
path: '$subjects.items'
}
}, {
$project: {
_id: '$subjects.items._id',
item: '$subjects.items'
}
}, {
$match: {
_id: ObjectId('5e6eaef8ae35a418f4f6dbd4')
}
}]
Then I tried translating this to C# with no success, this is as far as I've come:
var optionMenuItem = await collection.Aggregate()
.Unwind<OptionMenu, OptionMenuSubject>(i => i.Subjects)
.Unwind<OptionMenuSubject, OptionMenuItem>(i => i.Items)
.Match(i => i.Id == id)
.ToListAsync();
If somebody knows what I'm doing wrong or how I can pull this off it would be very much appreciated :)
