Reputation: 103
this is the query i have structured in mongo shell
db.vendormasters.aggregate([
{
'$match': {
status: 'active',
}
},
{ '$unwind': '$mappedToDealers'},
{
$lookup: {
from: "orders",
let: {
vendorId: "$_id",dealerId:'$mappedToDealers'
},
pipeline: [
{
$match: {
$and: [
{
$eq: [
"$vendorId",
"$$vendorId"
]
},
{
$eq: [
"$dealerId",
"$$dealerId"
]
}
]
}
}
], as: "orders"
}
},
{ '$unwind': '$orders' },
}]).pretty()
**the error message which i am getting in the shell is **
E QUERY [js] Error: command failed: {
"ok" : 0,
"errmsg" : "unknown top level operator: $eq",
"code" : 2,
"codeName" : "BadValue"
} : aggregate failed :
my collection structure is
//////collection 1 : vendorMasters///////////
{
"_id" : ObjectId("5e5642e32500b8273cbde3ac"),
"mappedToDealers" : [
ObjectId("5e1d82156a67173cb877f67d"),
ObjectId("5e5906dfc749dc4498033f7f")
],
"phoneNo" : 6#7159###,
"name" : "addedVendor8",
"address" : "Address6",
}
//////collection 2: orders///////////
{
"_id" : ObjectId("5e3a710af2657521e8c5668a"),
"date" : ISODate("2020-02-11T18:30:00Z"),
"order" : [
{
"_id" : ObjectId("5e3a710af2657521e8c5668c"),
"punchCount" : "###1",
"leavecCount" : 5,
},
{
"_id" : ObjectId("5e3a710af2657521e8c5668b"),
"punchCount" : "###1",
"leavecCount" : 5,
}
],
"vendorId" : ObjectId("5e5642e32500b8273cbde3ac"),
"dealerId" : ObjectId("5e1d82156a67173cb877f67d"),
}
{
"_id" : ObjectId("5e3a710af2657521e8c5668a"),
"date" : ISODate("2020-02-11T18:30:00Z"),
"order" : [
{
"_id" : ObjectId("5e3a710af2657521e8c5668c"),
"punchCount" : "###1",
"leavecCount" : 6,
},
{
"_id" : ObjectId("5e3a710af2657521e8c5668b"),
"punchCount" : "###1",
"leavecCount" : 2,
}
],
"vendorId" : ObjectId("5e5642e32500b8273cbde3ac"),
"dealerId" : ObjectId("5e5906dfc749dc4498033f7f"),
}
NOTE: there can be different vendorId's and different dealerId's in the documents if nothing matches then i should return an empty array , point out whats wrong in my query.my objective is find out all the orders from the orders collection which have matching vendorId & dealerId if it doesnot match than it shall return empty array
Upvotes: 0
Views: 595
Reputation: 59523
Your $match
condition just contains a logical expression:
$match: { $and: [...] }
However, it must contain a query. Try this one:
$match: { $expr: { $and: [...] } }
Upvotes: 1