'Getting error while running the aggregate method of MognoDB
I am getting the following error while joining two collection and project the fields using MognoDB.
Error::
Error: command failed: {
"ok" : 0,
"errmsg" : "'pipeline' option must be specified as an array",
"code" : 14,
"codeName" : "TypeMismatch"
} : aggregate failed
I am explaining my query below.
db.getCollection('orderproductsreport').aggregate([
{
$match: {
"Customer.StoreCode":"CMHB",
OrderStatus: 'Created',
'Customer.Children.StudentGrade': 'Grade 1',
OrderType: 'Online',
}
},
{
$lookup:{
from: "orders",
as: "PaymentInfo",
let: { OrderNumber: '$OrderNumber' },
pipeline: {
$match: {
$expr: {
$and: [
{ $eq: ['$OrderNumber', '$$OrderNumber'] },
{ $eq: ['$PaymentStatus', 'Payment Received'] },
]
}
}
}
}
},
{
$project:{
"OrderNumber":1,
"OrderStatus":1,
"OrderType":1,
"OrderCreatedAt": { $dateToString: { format: "%Y-%m-%d", date: "$OrderCreatedAt" } },
"StoreCode":1,
"ProductName":1,
"SKU":1,
"CategoryInfo.category.CategoryName":1,
"Customer.CustomerMobile":1,
"Customer.CustomerFirstName":1,
"Customer.CustomerLastName":1,
"Customer.Children.StudentAdmissionID":1,
"MRP":1,
"GST":1,
"TotalDiscountPrice":1,
"PaymentInfo.PaymentDetails.TransactionId":1,
"PaymentInfo.PaymentDetails.PaymentAmount":1,
"PaymentInfo.PaymentDetails.PaymentStatus":1,
}
}
])
Here I need to join two collection i.e-orderproductsreport and orders
and the common filed for both collection is OrderNumber
. I have to also match order collection
PaymentInfo.PaymentDetails.PaymentStatus
value with user input value along with rest match conditions. But As per my query its throwing error. I need to resolve this error and fetch the required record.
Solution 1:[1]
Use the aggregate method, not find
.
Solution 2:[2]
Change the pipeline
arg from an object to an array:
pipeline: { // No
$match: {
...
}
}
to
pipeline: [ // YES
{$match: {
...
}}
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | D. SM |
Solution 2 | Buzz Moschetti |