mongoDB: Aggregation - Is there a equivalent of the $lookup joins for the native node.js driver? -
mongodb: 2.1.3
after reading of aggregation enhancements coming mongodb 3.2 excited "$look" pipeline stage left-outer equi-joins.
unfortunately appears node driver doesn't have operator.(i don't see in native driver docs node , when tried use got error:
update: here code tried
var cursor = db.collection('messagethreads').aggregate([ {"$match": { _id: new objectid(threadid)} }, {"$lookup": { from: "messages", localfield: "_id", foreignfield: "threadid", as: "messageslist"} } ]); cursor.toarray(function(err,messages){ if(err) { res.status(500).json(error); } else if(convo === null){ res.status(400).end(); } else{ res.status(200).json(messages); } }); });
example - threadmessage document
{ "_id" : objectid("56b4f52c0e6368c00630aee6"), name: "messages 1" }
example - message document
{ "_id" : objectid("56b4f52c0e6368c00630af08"), "author" : "nick", "text" : "hello", "threadid" : objectid("56b4f52c0e6368c00630aee6") }, ...
intended result
{ "_id" : objectid("56b4f52c0e6368c00630aee6"), name: "messages 1", messagelist:[ { "_id" : objectid("56b4f52c0e6368c00630af08"), "author" : "nick", "text" : "hello", "threadid" : objectid("56b4f52c0e6368c00630aee6") }, ... ] }
" mongoerror: exception: unrecognized pipeline stage name: '$lookup' "
you can read more case joins here!
question: there intended way perform equivalent node.js native driver?
the "mongoerror" exception how driver reports error messages "server", , therefor such error indicates server connected not version suppport $lookup
, being 3.2 or greater:
$lookup
new in version 3.2.
performs left outer join unsharded collection in same database filter in documents “joined” collection processing. $lookup stage equality match between field input documents field documents of “joined” collection.
you can obtain server version connecting via serverstatus
database command. 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 package.json driver version pinned, show there 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 expected output supported server version:
3.2.0 [ { "_id": "56b4f52c0e6368c00630aee6", "name": "messages 1", "messageslist": [ { "_id": "56b4f52c0e6368c00630af08", "author": "nick", "text": "hello", "threadid": "56b4f52c0e6368c00630aee6" } ] } ]
so if listing not returning 3.2.x
on database connecting to, $lookup
pipeline operation not supported here , have resort alternate means, such pulling in "related" information "client side" instead.
Comments
Post a Comment