Can't restore the database - mongodb

I dump the mongodb database using mongodump --db dbName. Now when on different system I tried to restore it using mongorestore it is showing me error:
Failed: mindcentral.user: error creating collection mindcentral.user: error running create command: BSON field 'OperationSessionInfo.create' is a duplicate field
My dump folder is on desktop, so I am using command
mongorestore --db DBName Desktop/dump/DBName

You just Follow:
Mongorestore:
Syntax:
mongorestore -d databasename databasepath.
Example:
mongorestore -d user ./Desktop/dump/user
where the user is a database name.

Related

When using the Mongodump shell, how do you specify the database and collection from a replica set?

I need to create a mongodump of a specific collection from a specific database in a replica set. I am issuing the following command from the windows command shell:
mongodump --uri="mongodb://dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017/?replicaSet=dd-domain-dev-shard-0" --db=TargetDatabase_dev --collection=abcCollection
When doing so, the following error occurs:
error parsing command line options: illegal argument combination: cannot specify --db and --uri
How can I create an acceptable command that includes both the database and collection? I must specify both because the replica set contains several databases, and I only want that single collection in the output file.
MongoDb version: 4.2.11
MongoDump version: r4.0.1
Include the database name in the URI.
mongodump --uri="mongodb://dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017/TargetDatabase_dev?replicaSet=dd-domain-dev-shard-0" --collection=abcCollection
If that doesn't work:
mongodump --uri="mongodb://dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017/TargetDatabase_dev?replicaSet=dd-domain-dev-shard-0&authSource=admin" --collection=abcCollection
The previous answer is correct but you will need to include most options in the --uri string.
If you want to keep those as dashed options (--option) you can do it
by using --host instead.
mongodump --host="dd-domain-dev-shard-0/dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017" --db=TargetDatabase_dev
--collection=abcCollection
You will probably need to authenticate though, like so
mongodump --host="dd-domain-dev-shard-0/dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017/" --db=TargetDatabase_dev --collection=abcCollection --authenticationDatabase admin -u <username> -p <password>
Here is the relevant MongoDB docs

Restore Done but I dont see any database

I run this
mongorestore --db dbName
and I got
the --db and --collection args should only be used when restoring from
a BSON file. Other uses are deprecated and will not exist in the
future; use
--nsInclude instead
so I used
mongorestore --nsInclude 'dbName.*'
and I got
2018-01-06T21:28:02.106+0200 using default 'dump' directory
2018-01-06T21:28:02.142+0200 preparing collections to restore from
2018-01-06T21:28:02.147+0200 done
but I don't see any db of dbName that created.
I found the answer here Mongorestore of a db causing me trouble
mongorestore --db you_db_name --drop dump/you_db_name
Hope this helps.

how to copy a collection from one from another in robomongo

I have a collection named dashboard in one db and i want to copy that collection to another db using robomongo. How can i do this? I tried creating a new collection in 2nd db and tried copying but it failed. so somebody please help me
Another db - another connection. But Robomongo works only with one connection in one period of time. That is why it is impossible.
I suggest you to use mongoimport/mongoexoprt tools for your task. They comes with mongo, are located in same folder as mongod.exe and allows to move collections via databases, by exporting to and importing from a json file.
Code sample:
mongoexport --db testFrom --port portFrom --username userFrom --password passwordFrom --collection yourCollection --out test.json
mongoimport --db testTo --port portTo --username userTo --password passwordTo --collection yourCollection --file test.json

Can I restore metadata when restoring a MongoDB collection?

When I restore a collection with the following command:
mongorestore --db mydb --drop --collection mycollection --batchSize=100 mycollection.bson
as the original collection is dropped, the indexes are lost. I can see that there is also a mycollection.metadata.json file which contains indexes of this collection, but I cannot find in the documentation how this file can be restored.
All I've found is how to restore an entire database, which restores all collections with metadata from a directory. However, I want to restore only a single collection. How do I do that?
Note: I am using mongo version 3.0.7
You don't need to do something specific to restore metadata.
mongorestore does this for you.
When you restore collection:
mongorestore --collection mycollection --db mydb mycollection.bson
mongorestore checks directory where mycollection.bson exist for mycollection.metadata.json file. Just keep metadata file in the same directory as collection.

mongodump ignore some specified collections

I was trying to backup my mongo database on the product sever.and then restore then back to the staging server.
and here comes some problem, there are a lot of collections in db, I want to igonre some collections that I don't want to restore on staging server.
I can approach this by dumpping the staging db, dumpping the producting db, and then restore the prodct to staging useing --drop option. and restore the specified collections in staging db. uh..it's really bad.
1. dump producting db
mongodump --host product-server-host --username abcd --password bcda -d db -o pruduct-dump-dir
2. dump staging db
mongodump --host staging-server-host --username abcd --password bcda -d db -o staging -dump-dir
3. restore all collection, then restore the collection back
restore pruduct-dump-dir to staging server
mongorestore --host staging-server-host --username abcd --password bcda --drop pruduct-dump-dir
mongorestore --host staging-server-host --username abcd --password bcda --drop --collection coll pruducting-dump-dir
Is there any option like ignore-collection when I'm dumpping?
any suggestion will be appreciated :3
Now available from version 3.0.0
--excludeCollection <collection_name>
--excludeCollectionsWithPrefix <collection_prefix>
Repeat to exclude more than 1
Checkout the documentation
mongodump --db test --excludeCollection=users --excludeCollection=salaries
You can add --collection COLLECTION_NAME to dump the collection you need. By default, if you do not specify a collection to dump from a database, MongoDump will dump all collections in that database.
As of Mongo 3.4, you can now specify an --nsExclude <namespace pattern> option when restoring from a Mongo database dump, which will exclude the specified namespaces from the restore operation. This is particularly useful when the mongodump operation has already occurred.
Official documentation here: https://docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-nsexclude
You can exclude multiple collections with wildcards:
mongorestore --db test --nsExclude 'test.*_tmp'
Or alternatively, specifying multiple --nsExclude options work as well:
mongorestore --db test --nsExclude 'test.collection1' --nsExclude 'test.collection2'
I had to do the same while backing up a mongo db. If you use python (or any other language), you can use similar approach as well. After doing the mongodump, you simple have to remove the unwanted collection's bson & the metadata.json files.
import os
EXCLUDE_COLLECTIONS = ['collection_1', 'collection_2']
db_dump_path = "/Path/to/mongodump"
db_name = "name_of_db"
for collection_name in EXCLUDE_COLLECTIONS:
bson_file_path = os.path.join(db_dump_path, db_name, '{}.bson'.format(collection_name)
meta_file_path = os.path.join(db_dump_path, db_name, '{}.metadata.json'.format(collection_name)
if os.path.exists(bson_file_path) and os.path.exists(meta_file_path):
os.remove(bson_file_path)
os.remove(meta_file_path)