Kumaran
Kumaran

Reputation: 239

MongoDB - Transfer Data one Collection to Another Collection

I need to know how we can transfer the data or records from one collection to another collection in MongoDB and same time whether we can use "find()" condition while transferring the data from one to another collection.

If knows, share the details for our references.

Note:

Following error we are getting if fetching the records by using PHP from Large collection(number of rows having in this collection).

Error : "too much data for sort() with no index error"

That why we have discussed and transfer the data from one collection to another collection by using mongodb console.

Upvotes: 2

Views: 3036

Answers (2)

Justin Jenkins
Justin Jenkins

Reputation: 27080

First off, I'd suggest creating an index on whatever you are running your find() query on if possible, you can do this by using ensureIndex() ...

As for how to transfer the documents from the console:

> use source_database;
> var docs = db.source_collection.find({ something: 'foo' }); 
> use new_database;
> docs.forEach(function(doc) { db.new_collection.insert(doc) });

For more info, check out this blog post which explains the process in more detail.

Upvotes: 4

ravnur
ravnur

Reputation: 2852

You can do it directly in Mongo:

db.source.find(some_conditions).forEach(function(doc){db.dest.save(doc)})

Upvotes: 2

Related Questions