0
下面的代码首先在一个字段上连接两个集合,并尝试过滤其他字段上的值。
db.zeroDimFacts.aggregate(
{$lookup:{from:"zeroDim",localField:"Kind",foreignField:"Model", as:"EmbedUp"}},
{$project: {"EmSub":"$EmbedUp.Sub","Result": {$eq:["$Type","$EmbedUp.Sub"]}, "type":"$Type"}})
请检查以下输出的代码。即使'EmSub'&'$ Type'具有相同的值,但它不会显示在“结果”字段中。 如果是因为'EmSub'显示为数组,我如何比较只包含该数组中的值?
/* 1 */
{
"_id" : NumberLong(1),
"EmSub" : [
"Fruit"
],
"Result" : false,
"type" : "Fruit"
}
/* 2 */
{
"_id" : NumberLong(2),
"EmSub" : [
"Fruit"
],
"Result" : false,
"type" : "Fruit"
}
/* 3 */
{
"_id" : NumberLong(3),
"EmSub" : [
"Fruit"
],
"Result" : false,
"type" : "Fruit"
}
尽量$ EQ: “$类型”, “$ EmbedUp.Sub $。”] –
@AmiramKorach:得到下面的错误: 断言:命令失败:{ \t“ok”:0, \t“errmsg”:“FieldPath字段名称可能不以'$'开头。”, \t“code”:16410 }:聚合失败 –
对不起。尝试:“EmSub”:{$ first:“$ EmbedUp.Sub”}和比较$ eq:[“$ Type”,{$ first:“$ EmbedUp.Sub”}] –