Details
-
Bug
-
Resolution: Fixed
-
Major - P3
-
2.12.1
Description
Hey guys,
I found a problem with the following code:
namespace MongoError |
{
|
using System; |
using System.Collections.Generic; |
using System.Linq; |
|
|
using MongoDB.Bson; |
using MongoDB.Driver; |
|
|
public class Program |
{
|
public static void Main(String[] args) |
{
|
var client = new MongoClient(); |
var database = client.GetDatabase("MyDB"); |
var collection = database.GetCollection<Item>("Items") |
.AsQueryable();
|
|
var result = collection.SelectMany(x => x.Meta, |
(item, meta) => new ProjectedItem |
{
|
ItemId = item.Id.ToString(),
|
Meta = meta,
|
Property = item.Properties
|
.First(p => p.Id == meta.PropertyId)
|
});
|
|
|
Console.WriteLine(result);
|
}
|
|
|
private class Item |
{
|
public ObjectId Id { get; set; } |
public List<ItemMeta> Meta { get; set; } |
public List<ItemProperty> Properties { get; set; } |
}
|
|
|
private class ProjectedItem |
{
|
public String ItemId { get; set; } |
public ItemMeta Meta { get; set; } |
public ItemProperty Property { get; set; } |
}
|
|
|
private class ItemMeta |
{
|
public String Meta { get; set; } |
public Int32 PropertyId { get; set; } |
}
|
|
|
private class ItemProperty |
{
|
public Int32 Id { get; set; } |
public String Text { get; set; } |
}
|
}
|
}
|
The generated aggregation pipeline looks like this:
[
|
{
|
"$unwind": "$Meta"
|
},
|
{
|
"$project": {
|
"ItemId": {
|
"$toString": "$_id"
|
},
|
"Meta": "$Meta",
|
"Property": {
|
"$arrayElemAt": [
|
{
|
"$filter": {
|
"input": "$Properties",
|
"as": "p",
|
"cond": {
|
"$eq": [
|
"$$p._id",
|
"$$p.Meta.PropertyId"
|
]
|
}
|
}
|
},
|
0
|
]
|
},
|
"_id": 0
|
}
|
}
|
]
|
The condition part of the $filter statement is this:
"$eq": [
|
"$$p._id",
|
"$$p.Meta.PropertyId"
|
]
|
But I think it really should look like this:
"$eq": [
|
"$$p._id",
|
"$Meta.PropertyId"
|
]
|