下面我有一个汇总查询。我必须过滤掉Product collection上的聚合结果,因为对于某些客户来说,有大量的产品,在没有过滤器的情况下获取所有客户的产品(在单个聚合查询中)将导致Bson异常过大。
问题是我想要执行过滤器的字段之一是数组(
p.metadata.category
)Mongo$eg似乎不适用于数组字段
,它似乎只适用于简单的值和对象字段。
db.getCollection('customer').aggregate([
{
$lookup: {
from: 'Product',
localField: '_id',
foreignField: 'partiesList._id',
as: 'products',
}
},
{
$match: {
"_id": {$in: [
"C123",
"C456"
]
}
}
},
{
"$project": {
"products": {
"$filter": {
"input": "$products",
"as": "p",
"cond": {
$and:[
{
"$eq": ["$$p.metadata.category.name","somevalue"]
},
{
"$eq": ["$$p.isMain",true]
}
]
}
}
}
}
}
])
所以,上述查询的结果将是产品数组为空的客户列表(尽管产品实际上存在),但如果我删除
metadata.category.name
在上面的查询中,来自$和数组的条件像charm一样工作,p.isMain过滤器工作良好,按预期过滤掉产品,只显示isMain设置为true的产品。
以下是我的样本数据:
顾客:
{
"_id" : "C123",
"name" : "coooo"
}
产品(客户的产品):
{
"_id" : "PR123",
"isMain" : true,
"name" : "My Product",
"metadata" : {
"category" : [
{
"name" : "somevalue",
"version" : "1",
"referredType" : "Category",
"type" : "Category"
},
{
"name" : "someOtherValue",
"version" : "1",
"referredType" : "Category",
"type" : "Category"
}
]
},
"partiesList" : [
{
"_id" : "C123",
"role" : "Customer"
"referredType" : "Customer"
}
]
}
有什么想法或选择吗??