Show
Lee Theobaldunread, Feb 20, 2012, 6:16:25 AM2/20/12 to Hi all, Hi all. I was wondering what's the best way to copy items from one collection to another. As far as I can tell, I have 3 options:
What's the best to go for? #1 (db.eval) is a blocking action so I'd rather not take that option. #2 does the job but wraps the output in a value object that I don't
need/want. #3 seems like a very roundabout way of doing #1 but at least it doesn't block. We're using MongoDB to hold all the data we use for analytics. When a request is made to one of our websites, the data goes into a general "daily_data" collection. At the end of the day, we copy that days data from this collection into a monthly archive table (e.g. archive_201202). I'm trying to figure out the best way of performing this copy whilst having the least impact on the system. Thanks for any input, Scott Hernandezunread, Feb 20, 2012, 8:07:13 AM2/20/12 to 7 different ways to clone MongoDB connection
There are several ways how to clone a collection within the same or to a different MongoDB database. We'll test performance on replica set cluster in local network. The replica set consists of three MongoDB 4.2 members. Test collection collection1 contains 1 million documents with size about 500Mb. We'll consider seven different ways:
The results of our comparison you can find here. 1. db.collection.copyTo() commandUsage - run the command in Shell: db.collection1.copyTo("collection2") Execution time: 7m 5s (tested on MongoDB 4.0 cluster) 2. db.collection.find().forEach() commandUsage - run the command in Shell: db.collection1.find().forEach( function(docs){ db.collection2.insert(docs); }) Execution time: 7m 45s 3. db.collecion.aggregate() commandUsage - run the command in Shell: db.collection1.aggregate([{ $match: {} }, { $out: "collection2" }]) Execution time: 15s db.collection.aggregate() command reference 4. mongodump and mongorestore toolsThe mongodump and mongorestore tools are parts of the MongoDB tools package. Usage - run the command in command line: mongodump.exe --host <host> --port <port> --db test --collection collection1 --out "x:\out" mongorestore.exe --host <host> --port <port> --db test --collection collection2 "x:\out\test\collection1.bson" Execution time: 18s (9s mongodump + 9s mongorestore) 5. mongoexport and mongoimport toolsThe mongoexport and mongoimport tools are parts of the MongoDB tools package. Usage - run the command in command line: mongoexport.exe /host:<host> /port:<port> /db:test /collection:collection1 /out:collection1.json mongoimport.exe /host:<host> /port:<port> /db:test /collection:collection2 /file:collection1.json Execution
time: 44s (27s mongoexport + 17s mongoimport) 6. Duplicate collection tool of NoSQL Manager for MongoDBDuplicate Collection is a professional feature of NoSQL Manager for MongoDB Pro. It allows to duplicate collection very quickly within the same database. Right-click on collection1 collection in DB Explorer and select Duplicate 'collection1' Collection... item in the popup menu. Specify destination collection name, duplication parameters and click Duplicate. Execution time: 15s 7. Copy collection to another database tool of NoSQL Manager for MongoDBCopy Collection to another Database is a professional feature of NoSQL Manager for MongoDB Pro. It allows to copy one or many collections between databases and servers. Right-click on collection1 collection in DB Explorer and select Copy 'collection1' Collection to another Database... item in the popup menu. Specify destination database, additional parameters and click Execute. Execution time: 35s Summary
How do I transfer data from one collection to another in MongoDB?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.
How do I import collections in MongoDB?To import data to a MongoDB database, you can use mongoimport to import specific collections data, or you can use mongorestore to import a binary (BSON) full database backup. The exported database file must be stored locally on the same machine as your client.
How do I copy an index from one collection to another in MongoDB?The following commands works for this scenario:. Copy both index key and index options var indexes = db. user. getIndexes(); indexes. forEach(function(index){ delete index. v; delete index. ... . Copy index key only (batch processing) var indexKeys = db. user. getIndexKeys(); db. usertest. createIndexes(indexKeys);. |