Mongorestore writing to MongoDB datafiles - mongodb

MongoDB documentation has the following line. What do they mean when they say, mongorestore can directly write to MongoDB data files without an active mongod? Does it mean it can write even if a mongod instance is not ready and reachable, or, is there something called inactive mongod instance. I am finding it difficult to understand. Can someone explain me this?
mongorestore can write data to either mongod or mongos instances, in addition to writing directly to MongoDB data files without an active mongod.

Prior to v3.x mongorestore could be configured to write directly to data files rather than writing into data files via mongod.
The term "data files" here refers to the contents of a dbpath directory so, by specifying --dbpath you could tell mongorestore to attach to the data files in that directory and insert data into those files without having to go through a mongod instance.

Related

How to specificy path of new collection in Mongo DB?

I am running Mongo on Windows 10. Data access is made trugh Pymongo (Python). All my data is stored in an old large HDD, but I would like to have some smaller collection stored on a much faster SSD? When I create a new collection either trough Compass or Pymongo there is no option to specify the path on disk.
Is it possible to learn such power?
If you want to have databases in different disks under the same dbPath , this is the option:
Add the option --directoryperdb in the mongod config file or at startup.
db.createDatabase("newDatabase")
You will see inside the dbPath folder for the new database like:
\dbPath\newDatabase
Stop the mongodb process.
Copy the content from \dbPath\neWDatabase to your SSD let say:
ssd:\newData
make link to the folder with:
mklink /d \newData \dbPath\newDatabase
or follow this tutotial
Start the mongodb process again.
Note:
As suggested by #Wermfried in the comment it is safe to have the option --directoryperdb set initially in the member before to get populated ...

MongoDB databases within databases

I'd like to use a database within a database, so I have the dev DB with my development data in it and my test DB with all my test data. However, it doesn't look possible to put DBs inside of DBs with Mongo. So I assume an alternate solution is to create a different Mongo connection altogether for dev and test. So I have a dev_data directory and a test_data directory, each with its own mongod.lock file and each listening on a different port. Is that a good solution? How do I do that?
This is easily possible. You can start as many mongod processes as you want like this — just make sure the data directories exist and both data folder plus log file are writeable:
mongod --dbpath /opt/dev --port 27001 --logpath /var/log/mongodb/dev.log
mongod --dbpath /opt/prd --port 27002 --logpath /var/log/mongodb/prd.log
If this is really necessary or if multiple databases would be sufficient is another matter, but you'll know best what you require for your situation.

How to import data into MongoDB from another MongoDB?

I've installed new MongoDB server and now I want to import data from the old one. My MongoDB stores monitoring data and it's a bit problematic to export the data from old database (it's over 10Gb), so I though it might be possible to import directly from DB, but haven't found how to do that with mongoimport.
The export/import would be the fastest option.
But if you really want to bypass it you can use the new server as a replica of the old one, and wait for full replication.
It takes longer but it's an easy way to set up a full copy without impact on the first one.
Follow this:
http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/
And then, once it's done, change configuration again.
It's easiest than it seems, but I recommend you to do a dry run with a sample database before doing it...
Note that another benefit is that the new replica will be probably smaller in size than the initial database, because MongoDb is not very good at freeing space of deleted members
mongoimport/mongoexport is per collection operating, so it's not proper for this kind of operation.
Instead to use mongodump/mongorestore.
If the old MongoDB instance can be shutdown to do this task, you can shut down it then copy all data files to the new server as its own data. And run the new instance.
Also db.cloneDatabase() can handle it to copy data directly from old instance to new one. It should be slower against copying data files directly.
You can use mongodump and pipe directly to the new database with mongorestore like:
mongodump --archive --db=test | mongorestore --archive --nsFrom='test.*' --nsTo='examples.*'
add --host --port and --username to mongorestore to connect to the remote db.
db.cloneDatabase() has been deprecated for a while.
You can use the copydb command discribed here.
Copies a database from a remote host to the current host or copies a database to another database within the current host.
copydb runs on the destination mongod instance, i.e. the host receiving the copied data.

where is the mongodb database when the meteor instance is not running

In Meteor, each new application I create, it creates a new MongoDb instance and when I run the instance by typing "meteor" it is available until I stop Meteor.
I can save data etc. into this mongodb and the next time I boot up meteor, it should appear again.
So I'm just wondering, where are these mongodbs being stored? how can I access one when I don't have the meteor server running?
The actual data files are stored under the project directory under the path .meteor/local/db, so it really is just a matter of starting up an independent instance when your meteor server is not running:
$:~/myapp$ mongod --smallfiles --dbpath .meteor/local/db --port 30000
And of course with port and/or IP binding that does not conflict with another instance, and of course without the meteor process having started a mongod instance on this data already.
But you are probably better off just defining an external server to your project and just running that separately. Just define the environment variable so the startup process knows which mongodb instance to use:
export MONGO_URL=mongodb://your_host:27017/your_db
Or otherwise essentially replacing your_host and possibly the port as well as your_db with the target details of the server and database you want to use.

Data storage in mongodb

first of all please forgive me for asking a silly question but I am new to mongodb and just installed it on my windows platform by following this installation guide :http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
It says "MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is C:\data\db.You can specify an alternate path for data files using the --dbpath option to mongod.exe."
So I created a folder d://data/db in my computer and issued a command
C:\mongodb\bin\mongod.exe --dbpath d:\mongodb\data
Then it says
"At the mongo.exe prompt, issue the following two commands to insert a record in the test collection of the default test database and then retrieve that record:
db.test.save( { a: 1 } )
db.test.find()"
I issued this to commands to save and retrieve the objects and its working fine but what is this default test database? where is it? Moreover where this object is stored? Where I can find this file?
what is this default test database?
When you connect to a mongod server without specifying a database, a default database "test" is selected. Since databases are created lazily, it may not even exist until you write to it.
db.test.save( { a: 1 } )
After this line is executed, database with current name ("test" by default) is created (if didn't exist already) and in it, collection "test" is created (if didn't exist already).
where is it? Moreover where this object is stored? Where I can find this file?
All databases are ultimately stored as files in your data dir. Look for "test.*" files there.
mongod.lock, is the file which provides the PID of your running mongod instance. When you start a mongod instance, MongoDB check if the lock is empty to start cleanly mongod. Then MongoDB registered the PID number of the running mongod instance in this lock file.
MongoDB delete the contains of this lock file when you shutdown cleanly your server,
mongod --shutdown -- dbpath <path name> --port <port number>