Is there a simple way to do this?
If between two remote mongod instances, use
{ cloneCollection: "<collection>", from: "<hostname>", query: { <query> }, copyIndexes: <true|false> }
See http://docs.mongodb.org/manual/reference/command/cloneCollection/
use "Studio3T for MongoDB" that have Export and Import tools by click on database , collections or specific collection download link : https://studio3t.com/download/
Using pymongo, you need to have both databases on same mongod, I did the following:
db = original database
db2 = database to be copied to
cursor = db["<collection to copy from>"].find()
for data in cursor:
db2["<new collection>"].insert(data)
In my case, I had to use a subset of attributes from the old collection in my new collection. So I ended up choosing those attributes while calling insert on the new collection.
db.<sourceColl>.find().forEach(function(doc) {
db.<newColl>.insert({
"new_field1":doc.field1,
"new_field2":doc.field2,
....
})
});`
You can always use Robomongo. As of v0.8.3 there is a tool that can do this by right-clicking on the collection and selecting "Copy Collection to Database"
For details, see http://blog.robomongo.org/whats-new-in-robomongo-0-8-3/
This feature was removed in 0.8.5 due to its buggy nature so you will have to use 0.8.3 or 0.8.4 if you want to try it out.
There are different ways to do the collection copy. Note the copy can happen in the same database, different database, sharded database or mongod
instances. Some of the tools can be efficient for large sized collection copying.
Aggregation with $merge:
Writes the results of the aggregation pipeline to a specified collection. Note that the copy can happen across databases, even the sharded collections. Creates a new one or replaces an existing collection. New in version 4.2.
Example: db.test.aggregate([ { $merge: { db: "newdb", coll: "newcoll" }} ])
Aggregation with $out:
Writes the results of the aggregation pipeline to a specified collection. Note that the copy can happen within the same database only. Creates a new one or replaces an existing collection.
Example: db.test.aggregate([ { $out: "newcoll" } ])
mongoexport and mongoimport:
These are command-line tools.
mongoexport
produces a JSON or CSV export of collection data. The output from the export is used as the source for the destination collection using the mongoimport
.
mongodump and mongorestore:
These are command-line tools.
mongodump
utility is for creating a binary export of the contents of a database or a collection. The mongorestore
program loads data from a binary database dump created by mongodump
into the destination.
db.cloneCollection():
Copies a collection from a remote mongod
instance to the current mongod
instance.
Deprecated since version 4.2.
db.collection.copyTo(): Copies all documents from collection into new a Collection (within the same database). Deprecated since version 3.0. Starting in version 4.2, MongoDB this command is not valid.
NOTE: Unless said the above commands run from mongo
shell.
Reference: The MongoDB Manual.
You can also use a favorite programming language (e.g., Java) or environment (e.g., NodeJS) using appropriate driver software to write a program to perform the copy - this might involve using find and insert operations or another method. This find-insert can be performed from the mongo
shell too.
You can also do the collection copy using GUI programs like MongoDB Compass.