$lookup with ObjectID $let variable

2,666 views
Skip to first unread message

Steve Jerman

unread,
Jul 8, 2018, 1:31:29 PM7/8/18
to mongodb-user
Hi,

Using Mongo 3.6.5

I am using $lookup in an aggregation pipeline. Can't get this to work:

db.getCollection('sensor_data').aggregate(
[
   ....
   {$lookup:{
        from:'sites',
        let: { z: '$zone'},
        pipeline: [
             {$match: { _id : '$$z'}},           
        ],
        as: 'apInfo'
     }},
]

$zone is an ObjectID in the sensor_data document. 

If, I do this:

db.getCollection('sensor_data').aggregate(
[
   ....
   {$lookup:{
        from:'sites',
        let: { z: '$zone'},
        pipeline: [
             {$project: { t : '$$z'}},           
        ],
        as: 'apInfo'
     }},
]

I get what you would expect ... apInfo is an array with _id being the ObjectID and t being the zone...

{
    "_id" : {
        "mac" : NumberLong(260515773225939),
        "ts" : ISODate("2018-07-07T16:32:49.000Z")
    },
    "zone" : ObjectId("5af1b83c1c2d750008146c3f"),
    "apInfo" : [ 
        {
            "_id" : ObjectId("5ac3f91f3314940008ab61d3"),
            "t" : ObjectId("5af1b83c1c2d750008146c3f")
        },
        {
            "_id" : ObjectId("5af1b83c1c2d750008146c3f"),
            "t" : ObjectId("5af1b83c1c2d750008146c3f")
        },  ...

Any suggestions?

Steve



Steve Jerman

unread,
Jul 8, 2018, 1:35:58 PM7/8/18
to mongodb-user
OK, just answered my question..

This works:
             {$match: {   $expr : { $eq: [ "$$z", "$_id"] } }},

not sure why the simple version doesn't
Reply all
Reply to author
Forward
0 new messages