As far as I see all commands operate on the same database in mongodb. I want to do something like this:
db.mySourceCollection.find().forEach( function(x){ db.theDestinationCollection.save(x)} );
where mySourceCollection
is on liveDatabase
and theDestinationCollection
is on testDatabase
.
@Naman what is the use case of copy collection, i mean you need any command or it is ok with manually process? for the manual process just install studio3T connect both databases and right click on collection that you want to copy, click on option "Copy Collection" and then go to second database right click on " ...
As we know that in the mongo shell, documents are represented using curly braces ( {} ) and inside these curly braces we have field-value pairs. Now inside these fields, we can embed another document using curly braces {} and this document may contain field-value pairs or another sub-document.
The insert() Method To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.
use dbname
doesn't work in scripted mode (i.e. when scripting the shell with javascript), so you should use the db.getSiblingDB()
method instead to reassign the 'db' variable, e.g.:
db = db.getSiblingDB("otherdb")
More info here: http://www.mongodb.org/display/DOCS/Scripting+the+shell
Use use
:-)
> var documents = db.mySourceCollection.find()
> use testDatabase
switched to db testDatabase
> documents.forEach(function(x){ db.theDestinationCollection.insert(x) })
db
is used to refer to the currently connected database, however you can switch databases on the fly using the use
command, as I've shown above.
Check out the help
command in the shell -- it mentions this command and much more!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With