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?
Asked
Active
Viewed 211 times
0
-
what does ` all documents` mean here? – bhucho Dec 01 '20 at 17:40
-
Suppose my source collection has 1 million document, with 20 attributes in each document. I want to copy all the 1 million documents, but 2 specific attributes only, to the target collection. Iterating in a loop might take sometime, since the actual number of documents in in many millions. – Jayadevan Dec 02 '20 at 03:42
1 Answers
0
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'
),
)
);
}
);

Jayadevan
- 1,306
- 2
- 12
- 33