I am trying to import/restore a single collection from within MongoDB (i.e. mongorestore cannot be accessed, I think ...?).
Is it possible? What is the command? Ideally, I'd like to include indexes as well. The backup has been produced by mongodump.
Specifically, I am using the IntelliShell from the excellent MongoChef. I perform other commands in this as well, such as renaming existing collections first.
Related
I have a mongodb database with version 3.6.3. I have another mongodb database (on another machine) using version 4.4.5 with no documents in it. I want to put the data from the v3.6.3 into the v4.4.5 database. Can I safetly do this using mongoexport and then mongoimport or do I need to perform more steps?
Yes, mongoexport writes the documents out to a JSON file, and mongoimport can read that file and insert the documents to the new database.
These will transfer only the documents, but not index information. You many want to consider mongodump/mongorestore if you also need to move indexes.
Hello I have an ubuntu 14.04 server that is running mongodb 2.4.14. I need to move the mongo instance to a new server. I have installed mongo 3.4.2 on the new server and need to move the databases over. I am pretty new with mongo. I have 2 databases that are pretty big but when I do a mongo dump the file is nowhere near the site of the databases that mongo is showing.I cannot figure out how to get mongoexport to work. What would be the best way to move those databases? If possible can we just export the data from mongo and then import it?
You'll need to give more information on your issue with mongodump and what mongodump parameters you were using.
Since you are doing a migration, you'll want to use mongodump and not mongoexport. mongoexport only outputs a JSON/CSV format of a collection. Because of this, mongoexport cannot retain certain datatypes that exist in BSON and thus MongoDB does not suggest that anyone uses mongoexport for full backups; this consideration is listed on mongo's site.
mongodump will be able to accurately create a backup of your database/collection that mongorestore will be able to restore that dump to your new server.
If you haven't already, check out Back Up and Restore with MongoDB Tools
I have two environments DEV and STAGE and I need to take all of my collections from a DEV database to a database in STAGE.
Currently using mongodump to get all of my collections and indexes, which appears to work very well.
>mongodump --host 192.168.#.# --port 27019 --db MyDB
Then I am using mongorestore to populate STAGE with the appropriate collections and indexes.
>mongorestore --host 192.168.#.# --port 27019 --db test "C:\Program Files\MongoDB 2.6 Standard\bin\dump\MyDB"
My collections and indexes come across perfectly, however, my collection content comes as well. I have not found a way to exclude my actual data inside my collections...is this doable? Can I remove files from the output of mongodump to only have my collections and their 'indexes'.
MongoDB is schemaless so basically there is no point of restoring empty collections. Collections are created on the fly when you do a write.
I understand you just want to restore collection's metadata as indexes etc., I don't know what driver are you using but I would suggest you deal with this problem on the application level by writing a routine that creates the indexes etc.
Also removing the bson files and keeping only the metadata files from mongodump as you suggest will work (or at least it works in my case with mongo V 3.0.5 with wiredtiger engine) but is not documented as far as I know.
An other alternative could be to use -query option on mongodump to specify documents to include i.e: {_id:a_non_existing_id} but this option is applicable only to collection level dumps.
Im wondering how can I create a mongodump/mongorestore without backing up, restoring the indexes?
and how to incrementally restore a mongo db without restoring the indexes?
The mongodump utility creates a binary export of data from MongoDB and saves index definitions and collection options in a metadata.json associated with each database dumped. The index details do not take any significant space in your backup, and will normally be used by mongorestore to re-ensure indexes after each data for each collection is imported from a dump.
If you want to avoid creating any new secondary indexes after the restore completes, mongorestore has a --noIndexRestore option.
Note: The default _id index is required, and always created.
incrementally restore a mongo db without restoring the indexes?
The option for --noIndexRestore applies whether or not you are restoring into an existing database. If you mongorestore into an existing database with indexes using the --noIndexRestore option, no new index definitions will be added but existing indexes will still be updated as data is inserted.
Incremental backup & restore is really a separate question unless you have a simplistic use case: inserting new documents from successive dumps.
As at MongoDB 2.6, the mongorestore utility only inserts documents (i.e. there is no option for updates/upserts). You can use mongorestore to insert multiple dumps into an existing collection, but any documents causing duplicate key exceptions (eg. _id) will be skipped.
I would normally expect that an incremental backup & restore implies taking a delta of changes (all inserts/updates/deletions since a prior backup) and being able to re-apply those to an older copy of the same data. To achieve an incremental backup, you need a history of changes to data, which in MongoDB's case would be provided by the replica set operation log (oplog).
sudo mongorestore --db dbName ./dumpPath/ --noIndexRestore
I am creating an installation for a system using .NET and MongoDB. I wrote a batch to ensure indexes on DB but it would not work unless you have collection created. Is it a bad practice to do smth like:
db.Customers.save({username:"mkyong"})
db.Customers.remove({})
db.Customers.ensureIndex({SystemId:1,CampaignId:1,LocalIdentifier:1})
Use the createCollection command:
db.createCollection("Customers")
db.Customers.ensureIndex({SystemId:1,CampaignId:1,LocalIdentifier:1})