MongoDB storing data in two different locations - mongodb

I'm sort of new at MongoDB and running into a few problems with locating/accessing data that I've created or imported, in that it's ending up in two distinct locations.
If I start the shell like this
$ mongo
and then show the databases
$ show dbs
this gives me a list of about 10 databases that I've created. These are in the /data/db directory It does not include a db called 'pact'
However, if I connect like this
$ mongo localhost/pact
and then do
$show dbs
it only lists one db, the pact db, which isn't listed when I connect to mongo the other way by just doing 'mongo.' 'Pact' isn't in the /data/db directory. According to my notes, I might have made the 'pact' db by starting mongod this way,
mongod --dbpath data
which I would think would position it in the data/db directory, and I imported into the pact directory like this
mongoimport --stopOnError --db pact --collection products < products.json
Moving on, if I use mongo in irb and start like this
>> mongo_client = MongoClient.new("localhost", 27017)
and then do 'show dbs'
I get the long list of dbs from the /data/db directory (which didn't include pact).
In order to find db pact through irb, I tried to include it after localhost, as I do with mongo localhost/pact
>> mongo_client = MongoClient.new("localhost/pact", 27017)
but I got an error.
1 Is there a way I can find out what directory the 'pact' db is in?
2 How can I access it in irb with the Mongo driver if not by
mongo_client = MongoClient.new("localhost/pact", 27017)
which I assumed would work since I can do this in the shell mongo localhost/pact
3 Based on what I've told you, can you explain why this happened (I'm assuming it's not the proper way to have data saved in another directory)

My suggestion is to use mongodump in mongo localhost/pact shell context
mongodump -d pact -o /out/dir
to backup the entire database. And use mongorestore at normall mongo shell context
mongorestore -d pact /out/dir
to restore the database.

Related

Restoring a mongo database but mongo shell does not show it

mongodump was used long time ago to create a backup, now in order to restore the database for a Meteor app, this command was used:
ais2> mongorestore C:\Users\AAA\Documents\meteor\apps\dump\dump\
PS C:\Users\empl1\Documents\meteor\apps\ais2> mongorestore C:\Users\AAA\Documents\meteor\apps\dump\dump\
preparing collections to restore from
reading metadata for dbais2.dataTeckAllMatchCol from C:\Users\AAA\Documents\meteor\apps\dump\dump\dbais2\dataTeckAllMatchCol.metadata.json
reading metadata for dbais2.makeModelCol from C:\Users\AAA\Documents\meteor\apps\dump\dump\dbais2\makeModelCol.metadata.json
reading metadata for dbais2.usageCol from C:\Users\AAA\Documents\meteor\apps\dump\dump\dbais2\usageCol.metadata.json
reading metadata for dbais2.vehiclesDetailsCol from C:\Users\AAA\Documents\meteor\apps\dump\dump\dbais2\vehiclesDetailsCol.metadata.json
restoring dbais2.dataTeckAllMatchCol from C:\Users\AAA\Documents\meteor\apps\dump\dump\dbais2\dataTeckAllMatchCol.bson
restoring dbais2.makeModelCol from C:\Users\AAA\Documents\meteor\apps\dump\dump\dbais2\makeModelCol.bson
restoring dbais2.usageCol from C:\Users\AAA\Documents\meteor\apps\dump\dump\dbais2\usageCol.bson
restoring dbais2.vehiclesDetailsCol from C:\Users\AAA\Documents\meteor\apps\dump\dump\dbais2\vehiclesDetailsCol.bson
finished restoring dbais2.dataTeckAllMatchCol (11705 documents, 0 failures)
Since I have a directory "dbais2" with all the database files located in the above path.
In a new shell ais2> meteor mongo opens a mongo shell, but show dbs does not show the "dbais2". How can I use the newly restored database? or it was not restored correctly? if so. how to restore it correctly?
Thanks
When you run mongorestore as is, it will connect to the mongo instance running on port 27017 on your local machine (if any). That's what you would use in production. Since the restore succeeded, it must be that you have such an instance running. In that case, run mongo ais2 to connect to that instance and db.
In development, meteor runs its own mongo instance on port 3001 (assuming you used meteor on the default port 3000). When you run meteor mongo that is the instance your shell will connect to. If you want to restore into that, then rerun your command with the port specified:
mongorestore --port=3001 -d meteor C:\Users\AAA\Documents\meteor\apps\dump\dump\
After that you should see your data in the shell opened by meteor mongo. Note that in this command I'm also overriding the database name, so that the data will be imported into the database used by meteor in development (also called meteor).

How to clone a collection from one MongoDB to another on same server

I'm using Mongo 3.2. I have two databases on my localhost named client1 and client2.
Now client1 contains a collection named users.
I want to clone this collection to client2.
I have tried:-
use client2
db.cloneCollection('localhost:27017', 'client1.users',
{ 'active' : true } )
This outputs
{
"ok" : 0.0,
"errmsg" : "can't cloneCollection from self"
}
Is cloning a collection from one db to another on the same server prohibited?
Few things :
In general cloneCollection is used for different mongo instances but not to copy on same instances.
Also if you're using v4.2 you should stop using copyDB & cloneCollection cause they're deprecated compatibility-with-v4.2 & start using mongodump and mongorestore or mongoexport & mongoimport.
I would suggest to use mongodump & mongorestore :
Cause mongodump would preserve MongoDB's data types i.e.; bson types.
mongodump creates a binary where as mongoexport would convert bson to json & again mongoimport will convert json to bson while writing, which is why they're slow. You can use mongoexport & mongoimport when you wanted to analyze your collections data visually or use json data for any other purpose.
You can run below script in shell
declare - a collections = ("collectionName1" "collectionName2")
for i in "${collections[#]}"
do
echo "$i"
mongodump --host "All-shards" --username=uname --password password --ssl --authenticationDatabase admin --db dbname --collection "$i"
mongorestore --host=host-shard-name --port=27017 --username=uname --password=psswrd --ssl --authenticationDatabase=admin --db=dbname --collection= "$i" ./dump/dbName/"$i".bson;
done
To use mongodump, you must run mongodump against a running mongod or mongos instance. So these commands are being run expecting mongo is properly installed & path setup is good, if not you can navigate to mongo folder & run like ./mongodump & ./mongorestore. Above script will be useful if you wanted to backup multiple collections, You need specify few things in script like :
mongodump--host "All-shards" -> Here you need to specify all shards if your MongoDB is a replica set, if not you can specify localhost:27017.
mongorestore --host=host-shard-name -> You've to specify one shard of replica set, else your localhost, Few things here can be optional --ssl, --username, --password.
So mongodump will create a folder named dump for first time which will have the sub-folders with dbNames & each sub-folder will has bson files respective to their collection names dumped, So you need to refer dbName in restore command & collection name will be taken from variable i -> ./dump/dbName/"$i".bson
Note : MongoDB v3.2 is so old & in cloud based MongoDB service Mongo-atlas it has already reached it's end of lifecycle, So please upgrade asap. If you're looking for a free mongo instance or starting with MongoDB - you can try atlas.
db.cloneCollection() copies data directly between MongoDB instances.
https://docs.mongodb.com/v3.2/reference/method/db.cloneCollection/
That means you cannot clone inside the same mongod instance. Use mongoexport and mongoimport to clone your collection.
Since 4.2 MongoDb introduces $merge operator which allows copy from db1.collection to db2.collection.

MongoDB: mongorestore failing for small database

I am setting up a Mongo DB database on mongolab. I have 2 small documents in a single collection in my local db server, that I want to upload. I used mongodump to export that to a collection.bson and collection.metadata.json file
When I use mongorestore I get an error:
Failed:database.collection: error restoring from /tmp/mongodump/database/collection.bson: insertion error: EOF
collection.bson is less than 2KB. My research shows that this error shows up when your database is huge, but not very small. I can't get anything for my situation. The common solution of using --batchSize 1 results in the same issue.
After I run mongorestore the collection does exist on the remote, but it is empty (0 documents)
How can I get my tiny local db up on my remote server (on MongoLab).
I am locally running mongo3.4, but my MongoLab instance is 3.2.13. I'm wondering if the problem is the mismatch of versions?
mongorestore command:
mongorestore -h ds111882.mlab.com:11882 -d database -u username -p password /tmp/mongodump/database
mongodump
mongodump -d database -o /tmp/mongodump
Additional info: I imported the mongodump I created into another local database (also running 3.4) and it worked just fine, but restoring to the mongolab server causes the error.
Also a bsondump of the generated collection.bson file creates a json file with the correct 2 documents.
Found the answer, it is an issue with versions. My schema was using the Decimal128 type, which isn't supported in 3.2. Downgrading to Double (didn't need Decimal128 anyway) fixed the issue.

How to restore Mongo(WT engine) only with collection-0-****.wt file?

My mongodb can't lanuch now, when I want start mongo got error ***aborting after invariant() failure
Now I want to restore collection-0-****.wt file to a new db, is this possible?
As at MongoDB 3.2, only full backups of WiredTiger data directories can be copied into a new instance. WiredTiger collection or index files aren't self-contained; they rely on other metadata in the WiredTiger.* catalog files. The invariant/assertion you are getting on startup is expected if data files are incomplete or inconsistent.
If you want to backup and restore a single collection, you should use mongodump and mongorestore, eg:
mongodump --db test --collection northwind --host host1
mongorestore --db test dump/test/northwind.bson --host host2
For supported full backup procedures, see: MongoDB Backup Methods.
I had the same issue and after spending 5 hours doing everything, found this.
https://medium.com/#imunscarred/repairing-mongodb-when-wiredtiger-wt-file-is-corrupted-9405978751b5
You will need to restore 1 collection at a time(a few at once when you get the hang of it), but it works!

How to dump (backup) several tables (collection) and recover from MongoDB?

I'm new in MongoDB and my english is too bad - so, i can't understand mongodb documentation.
I have to dump (backup) several tables (collection) from MongoDB base - how I can do it?
Which utility I have to use for recover collections from backup?
Thank you for your attention for my question!
Start your mongod server. Assuming that your mongod server is running on localhost and port 27017. Now open a command prompt and go to bin directory of your mongodb instance and type the command:
mongodump
This command will back all data of the server to directory /bin/dump/
To backup only specific collection, use:
mongodump --collection YOUR_COLLECTION_NAME --db YOUR_DB_NAME
On a good-bye note, to restore/recover data use command:
mongorestore