Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb: db.collection.copyTo() and eval() have been deprecated. What's the alternatives?

Tags:

I find that db.collection.copyTo() and eval() have been deprecated since 3.0. But I do not find what can be instead.

What's the alternatives?

like image 543
Feng Yu Avatar asked May 27 '15 05:05

Feng Yu


People also ask

How do you copy all documents of a collection using query?

In MongoDB, copyTo() method is used to copies all the documents from one collection(Source collection) to another collection(Target collection) using server-side JavaScript and if that other collection(Target collection) is not present then MongoDB creates a new collection with that name.

What does save () in MongoDB return?

The save() returns a WriteResult() object that contains the status of the insert or update operation.


2 Answers

For a server-side solution you can use aggregation...

db.getCollection('source')     .aggregate([         { $out: 'destination' }     ]); 
like image 56
Guyon Avatar answered Oct 12 '22 12:10

Guyon


Per this discussion on the MongoDB Group.

The alternative is to implement the equivalent queries/operations using the normal MongoDB query language and client driver API

So that would mean writing your queries in a client environment (e.g. Node.js) and execute them that way. If run on the server connecting to localhost, they should pretty quick, although probably not as quick as using eval.

The rationale for the being deprecated is outlined in this ticket. https://jira.mongodb.org/browse/SERVER-17453

like image 40
Shane Stillwell Avatar answered Oct 12 '22 13:10

Shane Stillwell