Make data visible to MongoDB in copied files - mongodb

Sorry if my question is nooby, but I can't find any info about it over the net.
My situation is that I have only three files left from my previous DB (they are db_name.0, db_name.1 and db_name.ns), everything else from that system is lost. The DB version was 1.8 and the files were in /data/db.
Now I have a new DB (which is 2.0) on the new system, so I tried to put them to the new DB path, it seems that DB sees them, but doesn't eat (it doesn't say there's something wrong with DB, but records are nil). I know well there was some data. And I have it in app specs from the customer, so I can type manually this data in new DB again, but I'd like to know if there's a way to make this new DB see the old data in these files (not to do the work that had been done). I restarted mongod, tried mongod --repair — nothing. Is there a way to make MongoDB see these files?
And, now I have two pathes, /data/db and /var/lib/mongodb (which one is right to use?).

which one is right to use?
There is no "right one". When you start MongoDB, you have to point it to a DB folder by setting the --dbpath folder. It can really be any folder you want as long as the user who starts the mongod process also has access to that folder.
Is there a way to make MongoDB see these files?
The files you have listed are the default files created when a new DB is created. MongoDB allocates files in advance of needing them. So it's quite possible that these files are indeed empty.

Related

My MongoDB database was lost after running a read-only script

I use MongoDB 4.2 on my local machine (windows 10). I have not changed any configurations, so the default behavior of only accepting local connections should be in place. (I only need to access it locally)
I was running a script that was reading data from my MongoDB, there are no writes to the db in this script. When all the numbers were crunched I noticed weird results, and saw that my database was suddenly gone. I checked my dbpath and the data was gone from there too! Could it be a hack, or was it MongoDB that dropped both the database and the raw data in the dbpath?
I've seen similar questions on this forum, mostly resolved by the author forgetting to reroute to the correct dbpath, which is not the case here. I've checked the log but the log seems to be very limited (I restarted mongod and could only see logging happening after the restart).
MongoDB does not delete all of the files in its data directory.
Most likely either you are checking in the wrong place or something external to MongoDB deleted its files.

Copying DB's over in MongoDB

I am going to be doing some major DB restructuring in MongoDB that converts a bunch of records. I've ran this script against a copied DB locally and it works fine, so it should also work on the production database.
Is there an easy way of copying a DB instance into a new DB? I figured I could shut down MongoDB and copy the files into a new directory, rename them to the new DB, and then move them into the MongoDB data directory.
It's usually pretty slow to copy large DB files like that, so I wondered if there was a Mongo-specific way of copying DB's.
In case it is sufficient to copy only some collections, you can copy them as follows without shutting down the db server:
http://xmlquerying.blogspot.de/2012/10/copying-data-between-mongo-databases.html
Otherwise use mongodump and mongorestore.
http://docs.mongodb.org/manual/tutorial/backup-databases-with-binary-database-dumps/

Where are the db files? at /var/lib/mongodb I cant find any increase in size. I ran very big loop to create lakhs of objects

I am using UBUNTU, from /etc/mongod.conf, I found that /var/lib/mongdb is the path for data.
I have found some files like collectionname.0, .1, .ns in that directory. but when I run a very big loop(lakhs), I am able to get them back using mongo shell, but that mongodb directory size is not increasing, so there must be someother place where this data is being stored
What is that place?
There is no another place. As indicated by #itsbruce, in Ubuntu it's /var/lib/mongodb.
On a non-packaged installation (on Linux), i.e. without a /etc/mongodb.conf file, the default is /data/db/ (unless otherwise specified).
You can modify the switch "--dbpath" on the command line to designate a different directory for the mongod instance to store its data. Typical locations include: /srv/mongodb, /var/lib/mongodb or /opt/mongodb.
(Windows systems use the \data\db directory.) If you installed using a package management system.
I recommend using the db.collection.stats command as outlined here to monitor the size of your collection as you insert data. The link also explains what each field (in the output) means.
That is the correct data location for MongoDB on Ubuntu. MongoDB pre-allocates filespace. Are you sure you have generated more data than would fit into the initial pre-allocated files? Try blowing away any existing data files and restarting Mongo with the --noprealloc flag. Then add data.

MongoDB db files movement

I am using mongodb and I have changed the dbpath. Can I just copy all the files from the earlier dbpath to the new dbpath?
PS: I have come across the mongodump alternative which might be a bit tedious. So I am interested in knwoing whether plain copying work?
Yes, you can just do a normal copy if you shutdown your mongod first. You don't need to copy mongod.lock.

Hard to think of a reason why MongoDB doesn't create /data/db for us automatically?

I installed MongoDB both on Win 7 and on Mac OS X, and both places, I got mongod (the server) and mongo (the client).
But at both places, running mongod will fail if I double click on the file, and the error message was gone too quickly before I can see anything. (was better on Mac because Terminal didn't exit automatically and showed the error message).
Turned out it was due to /data/db not exist and the QuickStart guide says: By default MongoDB will store data in /data/db, but it won't automatically create that directory
I just have a big question that MongoDB seems to want a lot of people using it (as do many other products), but why would it not automatically create the folder for you? If it didn't exist... creating it can do not much harm... especially you can state so in the user agreement. The question is why. I can think of one strange reason, but the reason may be too strange to list here...
One good reason would be that you do not want it in /data/db. In this case, you want it to fail with an error when you forgot to specify the correct directory on the command line. The same goes for mis-spelled directory names. If MongoDB just created a new directory and started to serve from there, that would not be very helpful. It would be quite confusing, because databases and collections are auto-created, so there would not even be errors when you try to access them.