$查找管道中本地字段(嵌套字段)的 $match 值不是空字符串
$match value of local field (nested field) not empty string in $lookup pipeline
我无法在查找管道表达式上使用嵌套字段,以仅获取非空字符串值。
我正在执行一个包含 2 个查找阶段的聚合查询,其中第二个查找查询取决于第一个查找结果的嵌套字段。我真的无法弄清楚我的代码有什么问题。
invoice_lines_collection.aggregate([
{'$match':{'customer':'rtv'},
{'$lookup':{
'from':'products',
'localField':'ArticleNumber',
'foreignField':'number',
'as':'article_details'
}
},
{$unwind:{
'path':'$article_details',
'preserveNullAndEmptyArrays': true
}
},
{'$lookup':{
'from':'cutomers',
'let':{'group_id':'$article_details.group._id',
'customer':'$customer'},
'pipeline':{
{'$unwind':'$productgroups'},
{'$match':
{'$expr':
{$and:
['$ne':['$$group_id','2'],
'$eq':
['$productgroups.id','$$group_id'],
'$eq':['$name','$$customer']
],
}
}
}
},
'as':'customer_data'
}
}
])
invoice_lines=[
{
"_id" : ObjectId("5c885d21a202fc001103saf7"),
"ShipmentNumber" : "70727320006714asda4",
"Price" : 179.57,
"customer" : "test"
}
]
products = [
{
"_id" : ObjectId("2cv21eba4bd009f00161153b7"),
"number": "1234",
"group":{
"_id" :'',
"name" : '',
}
},
{
"_id" : ObjectId("5ca1eba4bd009f00161153b7"),
"number" : "2456",
"group" : {
"_id" : ObjectId("5ca29852bd009f00185553e3"),
"name" : "Test group",
}
}
]
customers = [
{
"_id" : ObjectId("5c6fd17a72ef146fcc29c6a1"),
"name" : "test",
"displayName" : "Test",
"productgroups" : [
{
"name" : "Test group",
"id" : ObjectId("5ca29852bd009f00185553e3"),
"markup" : 0.5
},
{
"name" : "Test group 2",
"id" : ObjectId("5ca29852bd009f0888554443"),
"markup" : 3.0
}
]
}
]
我只想拥有一个产品组,并且只获取商品属于一个组的发票行并获取组的详细信息。
当我 运行 上面的代码(转换为 PHP 语言)时,我得到
An object representing an expression must have exactly one field: { $ne: [ "", "$$group_id" ] ...
我弄明白了,这是一些语法错误,最后我不得不执行 $match 阶段以仅获取发票行中包含链接到产品组和我的代码的产品的结果现在看起来像这样:
db.getCollection('invoice_lines').aggregate([
{$match:{'customer':'rtv'}},
{$lookup:{
'from':'products',
'localField':'ArticleNumber',
'foreignField':'number',
'as':'article_details'
}
},
{$unwind:{
'path':'$article_details',
'preserveNullAndEmptyArrays': true
}
},
{$lookup:{
'from':'customers',
'let':{'group_id':'$article_details.group._id',
'customer':'$customer'},
'pipeline':[
{$unwind:'$productgroups'},
{'$match':
{'$expr':
{$and:
[
{$ne:['$$group_id','']},
{$eq:['$productgroups.id','$$group_id']},
{$eq:['$name','$$customer']}
],
}
}
}
],
'as':'customer_data'
}
},
{$match:{'customer_data':{$exists: true, $not: {$size: 0}}}}
我无法在查找管道表达式上使用嵌套字段,以仅获取非空字符串值。
我正在执行一个包含 2 个查找阶段的聚合查询,其中第二个查找查询取决于第一个查找结果的嵌套字段。我真的无法弄清楚我的代码有什么问题。
invoice_lines_collection.aggregate([
{'$match':{'customer':'rtv'},
{'$lookup':{
'from':'products',
'localField':'ArticleNumber',
'foreignField':'number',
'as':'article_details'
}
},
{$unwind:{
'path':'$article_details',
'preserveNullAndEmptyArrays': true
}
},
{'$lookup':{
'from':'cutomers',
'let':{'group_id':'$article_details.group._id',
'customer':'$customer'},
'pipeline':{
{'$unwind':'$productgroups'},
{'$match':
{'$expr':
{$and:
['$ne':['$$group_id','2'],
'$eq':
['$productgroups.id','$$group_id'],
'$eq':['$name','$$customer']
],
}
}
}
},
'as':'customer_data'
}
}
])
invoice_lines=[
{
"_id" : ObjectId("5c885d21a202fc001103saf7"),
"ShipmentNumber" : "70727320006714asda4",
"Price" : 179.57,
"customer" : "test"
}
]
products = [
{
"_id" : ObjectId("2cv21eba4bd009f00161153b7"),
"number": "1234",
"group":{
"_id" :'',
"name" : '',
}
},
{
"_id" : ObjectId("5ca1eba4bd009f00161153b7"),
"number" : "2456",
"group" : {
"_id" : ObjectId("5ca29852bd009f00185553e3"),
"name" : "Test group",
}
}
]
customers = [
{
"_id" : ObjectId("5c6fd17a72ef146fcc29c6a1"),
"name" : "test",
"displayName" : "Test",
"productgroups" : [
{
"name" : "Test group",
"id" : ObjectId("5ca29852bd009f00185553e3"),
"markup" : 0.5
},
{
"name" : "Test group 2",
"id" : ObjectId("5ca29852bd009f0888554443"),
"markup" : 3.0
}
]
}
]
我只想拥有一个产品组,并且只获取商品属于一个组的发票行并获取组的详细信息。
当我 运行 上面的代码(转换为 PHP 语言)时,我得到
An object representing an expression must have exactly one field: { $ne: [ "", "$$group_id" ] ...
我弄明白了,这是一些语法错误,最后我不得不执行 $match 阶段以仅获取发票行中包含链接到产品组和我的代码的产品的结果现在看起来像这样:
db.getCollection('invoice_lines').aggregate([
{$match:{'customer':'rtv'}},
{$lookup:{
'from':'products',
'localField':'ArticleNumber',
'foreignField':'number',
'as':'article_details'
}
},
{$unwind:{
'path':'$article_details',
'preserveNullAndEmptyArrays': true
}
},
{$lookup:{
'from':'customers',
'let':{'group_id':'$article_details.group._id',
'customer':'$customer'},
'pipeline':[
{$unwind:'$productgroups'},
{'$match':
{'$expr':
{$and:
[
{$ne:['$$group_id','']},
{$eq:['$productgroups.id','$$group_id']},
{$eq:['$name','$$customer']}
],
}
}
}
],
'as':'customer_data'
}
},
{$match:{'customer_data':{$exists: true, $not: {$size: 0}}}}