I cant use a nested field on an lookup pipeline expression, to get only the not empty string values.
I am doing an aggregate query with 2 lookup stages, where the second lookup query depends on a nested field of the result of the first lookup. I can't really figure out what the problem is with my code.
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
}
]
}
]
I want to have only one productgroup and to take only the invoice lines where article belongs to a group and get the group details.
When I run the above code (converted in PHP language) I get
An object representing an expression must have exactly one field: { $ne: [ "", "$$group_id" ] ...
I figured it out, it was some syntax error and in the end i had to do a $match stage to take only the results where a an invoice lines has a product which is linked to a product group and my code now looks like this :
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}}}}