聚合$unwind,$lookup在mongodb中不起作用

聚合$unwind,$lookup在mongodb中不起作用,mongodb,aggregation-framework,Mongodb,Aggregation Framework,这是我在mongo shell中构建的查询 db.vendormasters.aggregate([ { '$match': { status: 'active', } }, { '$unwind': '$mappedToDealers'}, { $lookup: { from: "orders", let: {

这是我在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()
**我在shell中得到的错误消息是**

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"),
        }

注意:文档中可能有不同的vendorId和dealerId,如果没有匹配项,那么我应该返回一个空数组,指出我的查询中的错误。我的目标是从orders集合中找出所有具有匹配vendorId和dealerId的订单,如果不匹配,它将返回空数组

您的
$match
条件只包含一个逻辑表达式:

$match: { $and: [...] }
但是,它必须包含一个查询。试试这个:

$match: { $expr: { $and: [...] } }

@-Wernfried Domscheit我已经添加了>>>>管道:[{$match:{$and:[{$eq:[“$vendorId”,“$$vendorId]},{$eq:[“$dealerId”,“$$dealerId”]}}],作为:“订单”“>>>rest代码完全相同它仍然给出完全相同的错误,必须有其他错误issue@sachin:您的
$match
必须是这样的
[{$match:{expr:{$and:[{$eq:[“$vendorId”,“$$vendorId”]},{$eq:[“$dealerId”,“$$dealerId”]}}}]
,请检查: