How to specificy path of new collection in Mongo DB? - mongodb

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 ...

Related

Mongorestore writing to MongoDB datafiles

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.

Where are mongo replication settings stored?

After a complete reinstall of mongo, it has still remembered the replica set configuration from before; where has it stored this?
I installed mongo on a linux server, using our project's user account, into a directory owned by that user. I set up replication and had it working fine. Then, to test out some finer install points, I removed the whole mongo directory and did a reinstall. Entering into mongo, I found that the replication was already set up as before; so it would appear that mongo is storing the information somewhere.
I have checked several areas which might have been holding the replica set config:
First, in the mongo directory, but that was deleted.
In some traditional linux structure probably owned by root, but the project user does not have root access and mongo, run by the same, should not either.
The project user's home directory. Now this does have a .dbshell file containing the command line history, but only that. I did not see any other files there that related.
Some location specified in the mongo configuration. But I only have two paths mentioned in there, one for the system log (systemLog.path) and the other for storage (storage.dbPath), and the both point to the mongo directory, which was deleted.
Does anyone know where mongo is storing this configuration information?
The replication data is stored in the local database in each node (as of MongoDB 3.2.9). This database contains information about the replica set, and also contains the oplog (the oplog.rs collection). The replica set information is stored in the system.replset collection in this local database.
The physical files for this database (and also for other databases) are stored in the dbPath directory, which can be configured using:
The --dbpath parameter when starting mongod
The storage.dbPath setting in the configuration file
The default dbpath value is /data/db
Your replica set setting will not be retained if you emptied the dbpath directory. If you find that the replication settings are being retained, it is possible that the dbpath setting is incorrect.

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>

Restoring from a single instance mongodb to an empty replica set

I have a mongodb installed on windows server. I take regular backups of the data/db folder using Rackspace backup.
I created a deployment of a mongodb replica set with 3 ubuntu servers using Rackspace deployments. Now I want to move the data on windows to the empty replica set. How can I do it?
I tried copying the contents of data/db on windows to var/lib/mongodb on the primary replica set. It didn't work.
For some reason the var/lib/mongodb on the ubuntu machines does not contain data/db directory. When I create a new db the db files are created on var/lib/mongodb directory.
The difference in data directories is fine .. on Windows the default dbpath will be c:\data\db; the Ubuntu package sets the dbpath to /var/lib/mongodb instead.
Since you are starting with an empty replica set (and using a backup from a standalone server), the most straightforward approach would be to:
Stop all the mongod servers for the replica set (you definitely don't want to copy data files directly into a running instance!).
Remove any files that are already in the /var/lib/mongodb data directory.
Copy the data files from your standalone MongoDB backup into /var/lib/mongodb on one of your replica set servers. This server will become your primary to set up the rest of the replica set.
Start up this primary making sure to include a replSet name in your configuration file. You may already have this set from your "empty" replica set that you already created.
Run rs.initiate() in the mongo shell to create the initial configuration on the primary.
Start up your additional servers as members of this replica set: they need the same replSet name configured.
Use rs.add(..) to add your additional servers from the mongo shell on your primary. Assuming the add is successful (i.e. the mongods can connect to each other), this will begin the process of initial sync (copying data from the primary) and the new hosts will become secondaries after they have finishing initial sync.
This is essentially the same steps as the deploy a replica set tutorial, except you are copying over your data first.
The problem could be related to the configuration file of mongodb
locate the file mongodb.conf and edit the dbpath parameter, check if the path really exist, and if it doesn't create the missing directories. Also check permissions in that path
anyway i don't know if its the right way to just copy the datafiles in a new location, i guess you should use mongo import/export

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.