laravelmongodbeloquentlumenjenssegers-mongodb

How can I copy data from one MongoDB collection to another using jenssegers


I wanted to copy specific attributes from all documents in one MongoDB collection to another. I am using Lumen (v6.0.2) with jenssegers/mongodb(3.6.0). Is it possible to do it without looping through the documents?


Solution

  • Figured out. Assuming that I wanted to copy 2 attributes, ID and updated_at from sourcecollection to targetcollection

    DB::collection('sourcecollection')->raw(function($collection) {
     return $collection->aggregate(array(
     array(
     '$project' => array(
      'ID' => 1,
      'updated_at' => 1
     )),
     array(   
      '$out' => 'targetcollection'
     ),
     )   
     );
    }
     );