intekhabrizvi / Codeigniter-mongo-library

MongoDB Library for Codeigniter with advance features like aggregation, journal, write concern and query profiling
http://intekhabrizvi.github.io
149 stars 142 forks source link

aggregate ($lookup) support. (aka Join) #10

Closed bencagri closed 8 years ago

bencagri commented 8 years ago

Hi there, this is an awesome library. But I think it would be much more awesome with mongos join methods. https://www.mongodb.com/blog/post/joins-and-other-aggregation-enhancements-coming-in-mongodb-3-2-part-1-of-3-introduction

Sample schema.

db.left.find()
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2b"), "name" : "Rod", "ref" : 1 }
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2c"), "name" : "Jane", "ref" : 1 }
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2d"), "name" : "Freddy", "ref" : 2 }

db.right.find()
{ "_id" : 1, "rightData" : "One" }
{ "_id" : 2, "rightData" : "Two" }

db.left.aggregate(
[{
    $lookup:
    {
        from: "right",
        localField: "ref",
        foreignField: "_id", 
        as: "stuffFromRight" 
    }
}])
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2b"), "name" : "Rod", "ref" : 1, "stuffFromRight" : [ { "_id" : 1, "rightData" : "One" } ] }
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2c"), "name" : "Jane", "ref" : 1, "stuffFromRight" : [ { "_id" : 1, "rightData" : "One" } ] }
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2d"), "name" : "Freddy", "ref" : 2, "stuffFromRight" : [ { "_id" : 2, "rightData" : "Two" } ] } 
intekhabrizvi commented 8 years ago

Hello, You can use aggregate function of library to perform join query for example. $ops = array( array('$lookup'=> array('from'=>'right', 'localField'=> 'ref', 'foreignField'=>"_id", 'as'=> "stuffFromRight")) ); $this->mongo_db->aggregate('left', $ops);

previateri commented 6 years ago

Hello, I am trying to perform an aggragate with $opp $lookup to 'join' two collections and I have with return the error: "The 'cursor' option is required, except for aggregate with the explain argument"

Can you help me solve it? @intekhabrizvi @bencagri

mehmetpolat commented 5 years ago

Hello, I am trying to perform an aggragate with $opp $lookup to 'join' two collections and I have with return the error: "The 'cursor' option is required, except for aggregate with the explain argument"

Can you help me solve it? @intekhabrizvi @bencagri