MongoDB databases within databases - mongodb

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.

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

MongoDB storing data in two different locations

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.

Mongo and creating a new database in dev with a reduced size

I use the below to create a new database in mongo
sudo mkdir -p /data/db2/
When I start mongo e.g.
mongod --port 27019 --dbpath /data/db2 --replSet rtb/test:27017 --rest
Mongo creates a 3 gig file. I am in dev and to reduce the size to e.g. 100 Megs. How to I do that?
MongoDB will pre-allocate data files. when it starts running. By default a new database will contain the following files (where test is the name of the DB)
test.ns (16MB)
test.0 (16MB)
test.1 (32MB)
As more files are needed, the pattern continues: 64MB, 128MB, 256MB, 1024MB, 2048MB. After 2GB, each new file will be 2GB.
In your case, you are running a replica set. Replica Sets require a database called local. This database contains the oplog which is used replication. The oplog is a capped collection.
The capped collection is a special collection which must be pre-allocated. In your case it is likely defaulting to about 3GB.
You can control the size of the local database using the --oplogSize parameter.
If you want to keep your local copy small you can also use the --smallfiles option which will slow down the speed of pre-allocation.