mongoDB: Aggregation - Is there a equivalent of the $lookup joins for the native node.js driver? mongoDB: Aggregation - Is there a equivalent of the $lookup joins for the native node.js driver? mongodb mongodb

mongoDB: Aggregation - Is there a equivalent of the $lookup joins for the native node.js driver?


The "MongoError" exception is how the driver reports any error messages from the "server", and therefor such an error indicates that the server connected to is not a version that suppport $lookup, being 3.2 or greater:

$lookup

New in version 3.2.

Performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing. The $lookup stage does an equality match between a field from the input documents with a field from the documents of the “joined” collection.

You can always obtain the server version you are connecting to via the serverStatus database command. Also in full reproducible listing:

var async = require('async'),    mongodb = require('mongodb'),    MongoClient = mongodb.MongoClient,    ObjectId = mongodb.ObjectId;MongoClient.connect("mongodb://localhost/test",function(err,db) {  async.series(    [      function(callback) {        db.command({ "serverStatus": 1 }, function(err,status) {          console.log(status.version);          callback(err);        });      },      function(callback) {        async.each(['threadmessage','message'],function(colname,callback) {          db.collection(colname).remove({},callback);        },callback);      },      function(callback) {        db.collection('threadmessage').insert(          {            "_id" : ObjectId("56b4f52c0e6368c00630aee6"),            "name": "Messages 1"          },          callback        );      },      function(callback) {        db.collection('message').insert(          {            "_id" : ObjectId("56b4f52c0e6368c00630af08"),            "author" : "Nick",            "text" : "Hello",            "threadID" : ObjectId("56b4f52c0e6368c00630aee6")          },          callback        );      },      function(callback) {        var cursor = db.collection('threadmessage').aggregate([          { "$lookup": {            "from": "message",            "localField": "_id",            "foreignField": "threadID",            "as": "messagesList"          }}        ]);        cursor.toArray(function(err,result) {          console.log(JSON.stringify(result,undefined,2));          callback(err);        });      }    ],    function(err) {      if (err) throw err;      db.close();    }  );});

As well as the package.json with the driver version pinned, just to show there is no driver version issue:

{  "name": "lookup",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1"  },  "author": "",  "license": "ISC",  "dependencies": {    "async": "^1.5.2",    "mongodb": "2.1.3"  }}

Gives the expected output with a supported server version:

3.2.0[  {    "_id": "56b4f52c0e6368c00630aee6",    "name": "Messages 1",    "messagesList": [      {        "_id": "56b4f52c0e6368c00630af08",        "author": "Nick",        "text": "Hello",        "threadID": "56b4f52c0e6368c00630aee6"      }    ]  }]

So if that listing is not returning 3.2.x on the database you are connecting to, then the $lookup pipeline operation is not supported here and you would have to resort to alternate means, such as pulling in the "related" information "client side" instead.