我尝试对左边连接的3个值做一些数学函数,收集的值在右边的集合中。
.forEach
我正在尝试将值提升到顶级文档中,因为我在数学函数方面遇到麻烦,抱怨点引用字段。
在上面的文档中我想要d = sqr((x1-x2)2+(y1-y2)2+(z1-z2)2)。
我试图在查询期间执行此操作而不是重新处理整个集合,有100,000个条目(可能)
代码在PowerShell中,带有.net驱动程序,很高兴有.js但是
Out out如下......
$AllBodies=Invoke-MdbcAggregate @(
# filter bodies with arsenic
@{ '$match' = @{
'name' ='Gru Hypue KS-T d3-31 9 b a'
'materials' = @{ '$elemMatch'=@{ 'material_name'="Arsenic" } }
}}
# list just the name, materials array and FK
@{ '$project' = @{
'system_id'=1;
'name'=1;
'materials'=1;
}}
# unwind the array, duplicates source record
@{ '$unwind' = @{
'path' = '$materials';
'preserveNullAndEmptyArrays' = $false
}}
# now re-filter the for just the material arsenic/
@{ '$match' = @{
'materials.material_name' = 'Arsenic';
'materials.share' = @{ '$gte' = 2.7 }
}}
# sort the records by % arsenic descending
@{ '$sort' = @{
'materials.share' = -1
}}
# left join the systems collection
@{ '$lookup' = @{
'from' = 'systems'
'localField' = 'system_id'
'foreignField' = 'id'
'as' = 'systems'
}}
# add fields to work with later - more dev required
@{ '$addFields' = @{
'x' = '$systems.x'
'y' = '$systems.y'
'z' = '$systems.z'
'systemname' = '$systems.name'
}}
) -Collection $bodies
如您所见,x,y,z是集合而不是值
TIA
答案 0 :(得分:0)
如果我理解正确,您只需将最后一个阶段更改为:
@{ '$addFields' = @{
'x' = @{ '$arrayElemAt' = @( '$systems.x', 0 ) }
'y' = @{ '$arrayElemAt' = @( '$systems.y', 0 ) }
'z' = @{ '$arrayElemAt' = @( '$systems.z', 0 ) }
'systemname' = '$systems.name'
}}
这将只为您提供每个数组的第一个值。