MongoDB聚合$ elem在$ lookup阶段匹配 [英] MongoDB aggregation $elemMatch inside $lookup stage
问题描述
我有一个看起来像这样的MongoDB集合:
I have a MongoDB collection that is looking like this:
{
players: [
{uuid: "A"},
{uuid: "B"}
]
},
{
players: [
{uuid: "A"},
{uuid: "C"}
]
},
{
players: [
{uuid: "D"},
{uuid: "E"}
]
}
我想使用上一个聚合阶段的结果,现在使用 $ lookup
阶段查找播放器出现的所有文档:
I want to use results of a previous aggregation stage and now find all documents where a player shows up, using a $lookup
stage:
from: "collection",
pipeline: [
{
$match: {
players: {
$elemMatch: {
uuid: "$playerId"
}
}
//using "players.uuid": "$playerId" doesn't work either
}
}
],
as: "field"
我的 $ lookup
阶段的输入如下:
{
"playerId" : "A"
}
{
"playerId" : "B"
}
{
"playerId" : "C"
}
此查询在 field
中返回一个空数组.似乎 $ uuid
没有得到正确的评估,因为如果我将 $ uuid
与硬编码的值(例如 A
)交换,此查询将返回结果.
This query returns an empty array in field
. It seems like $uuid
is not getting evaluated correctly, because if I exchange $uuid
with a hardcoded value (e.g. A
), this query returns results.
我也尝试过使用 let
属性,这给了我相同的结果.我在做什么错了?
I have also tried using the let
property, this gave me the same result.
What am I doing wrong?
推荐答案
使用您提供的文档.我相信这可能对您有用:
Using the documents you've provided. I believe this might work for you:
我已经使用 $ lookup
对包含 playerId
的集合进行了联接,该集合创建了一个名为的
.然后,我使用 array
字段 $ unwind
从 field
和 player
中提取所有数组元素.最后,我使用 $ cond
来交叉检查两个值是否匹配.
I've used $lookup
to do a join onto the collection which holds the playerId
, which creates an array
called field
. I then use $unwind
to extract all the array elements from both field
and player
. Finally I use $cond
to crosscheck if both values match.
db.getCollection('foo').aggregate([
{ $lookup : {
from: "bar",
localField: "players.uuid",
foreignField: "playerId",
as: "field"
} },
{ $unwind : "$players" },
{ $unwind : "$field" },
{ $project : {
"players": 1,
"field" : 1,
"isMatch": {
"$cond": [ { "$eq": ["$players.uuid", "$field.playerId"] }, 1, 0 ]
} } }
])
我特意留了输出详细信息.
I've purposely left the output verbose..
/* 1 */
{
"_id" : ObjectId("5a7f534b337e8d2b97ff2ffb"),
"players" : {
"uuid" : "A"
},
"field" : {
"_id" : ObjectId("5a7f5374337e8d2b97ff2ffe"),
"playerId" : "A"
},
"isMatch" : 1.0
}
/* 2 */
{
"_id" : ObjectId("5a7f534b337e8d2b97ff2ffb"),
"players" : {
"uuid" : "A"
},
"field" : {
"_id" : ObjectId("5a7f539b337e8d2b97ff2fff"),
"playerId" : "B"
},
"isMatch" : 0.0
}
/* 3 */
{
"_id" : ObjectId("5a7f534b337e8d2b97ff2ffb"),
"players" : {
"uuid" : "B"
},
"field" : {
"_id" : ObjectId("5a7f5374337e8d2b97ff2ffe"),
"playerId" : "A"
},
"isMatch" : 0.0
}
/* 4 */
{
"_id" : ObjectId("5a7f534b337e8d2b97ff2ffb"),
"players" : {
"uuid" : "B"
},
"field" : {
"_id" : ObjectId("5a7f539b337e8d2b97ff2fff"),
"playerId" : "B"
},
"isMatch" : 1.0
}
/* 5 */
{
"_id" : ObjectId("5a7f5356337e8d2b97ff2ffc"),
"players" : {
"uuid" : "A"
},
"field" : {
"_id" : ObjectId("5a7f5374337e8d2b97ff2ffe"),
"playerId" : "A"
},
"isMatch" : 1.0
}
/* 6 */
{
"_id" : ObjectId("5a7f5356337e8d2b97ff2ffc"),
"players" : {
"uuid" : "A"
},
"field" : {
"_id" : ObjectId("5a7f53a8337e8d2b97ff3000"),
"playerId" : "C"
},
"isMatch" : 0.0
}
/* 7 */
{
"_id" : ObjectId("5a7f5356337e8d2b97ff2ffc"),
"players" : {
"uuid" : "C"
},
"field" : {
"_id" : ObjectId("5a7f5374337e8d2b97ff2ffe"),
"playerId" : "A"
},
"isMatch" : 0.0
}
/* 8 */
{
"_id" : ObjectId("5a7f5356337e8d2b97ff2ffc"),
"players" : {
"uuid" : "C"
},
"field" : {
"_id" : ObjectId("5a7f53a8337e8d2b97ff3000"),
"playerId" : "C"
},
"isMatch" : 1.0
}
这篇关于MongoDB聚合$ elem在$ lookup阶段匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!