How do I specify a repair path from within a mongo shell? - mongodb

How can I specify a repair path for Mongo (v2.2.0) from within the mongo shell?
For example, I could normally run the command:
mongod --repair --repairpath /opt/vol2/data
How could I specify the repair path if I use the following syntax from within the shell:
db.repairDatabase()

The repairDatabase command will be executed on the database that you are currently connected to and by extension at the path that the current database's data resides.
You can define which database to use by using the use [dbname] command from within the mongo shell:
Taken from the docs:
use <db>
Switch current database to <db>. The mongo shell variable
db is set to the current database.
Alternatively, you can specify which database to use when starting the actual shell. For example, if you wanted to connect to a shell on the my_db database that is running on port 27016, you would launch the mongo shell with the following command:
$ mongo my_db
To find out which database you are currently using, you can simply type db in the shell and you'll get the name of the current 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 do I set the default connection URL for mongo CLI?

I cannot find anywhere to set the default connection URL for the mongo CLI. I want to be able to specify a particular username/password/hostname/database, which can all be specified as a URL. However, I want to just be able to type mongo instead of mongo "mongodb://…" in the shell.
I also don’t want the password to show up in /proc/«PID»/cmdline, but having it in a dotfile in my home directory is perfectly fine with me.
It doesn’t appear that ~/.mongorc.js allows specifying the default connection string. But I would expect such an option to be available because the mysql CLI supports ~/.my.cnf which allows you to specify username/password/hostname/database. So, where is this for mongodb?
EDIT: The solution has to work even if the mongodb at localhost is fully secured (no insecure local test database should be required—I thought this went without saying).
You can run mongo shell commands in ~/.mongorc.js. So you can just run the connect method in your ~/.mongorc.js:
db = connect("mongodb://username:password#host:27017/myDatabase")
When you run mongo it will first connect to localhost, then connect to the database passed to the connect method.
A MongoDB server must be running on localhost, otherwise the mongo shell will error and exit before trying to connect to the other DB.
Run mongo --nodb to get around needing to connect on localhost before connecting to whatever is defined in ~/.mongorc.js.
To directly launch mongo against a particular hostname/database/username/password without prompting for a password, without passing the password on the CLI, and without requiring an insecure database at mongodb://localhost/test, you can use this technique. Write a script file which does the connection for you and then launch mongo with the appropriate arguments. For example:
~/.mongo-do-auth.js
// https://docs.mongodb.com/manual/reference/program/mongo/#cmdoption-nodb
// https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#mongo-shell-new-connections
//
// Run this with:
//
// mongo --nodb --shell ~/.mongo-do-auth.js
db = connect('localhost/admin', 'root', 'asdf123');
And then, simply launch it from the CLI to get an interactive session:
sam ~ # mongo --nodb --shell ~/.mongo-do-auth.js
MongoDB shell version: 2.6.12
type "help" for help
connecting to: localhost/admin
> quit()
Or, to run a script noninteractively, simply remove --shell and append your script. Consider a script:
s2.js:
print('connected to database ' + db);
To run:
sam ~ # mongo --nodb ~/.mongo-do-auth.js ~/s2.js
MongoDB shell version: 2.6.12
loading file: /root/.mongo-do-auth.js
connecting to: localhost/admin
loading file: /root/s2.js
connected to database admin
However, this requires a lot of work and invoking mongo with arguments. It appears that this is a limitation of mongo because no one has suggested any alternative which works as smoothly as the mysql CLI client’s ~/.my.cnf. This means that all shell scripts which directly invoke mongo will need to allow the the user to pass through --nodb and a path to a connection script instead of being able to rely on implicit per-user configuration.

MongoDB and Batch script operation

I am trying to run MongoDB queries from batch script in windows...
I can not find any reference how to do that. Please can you show me some sample commands and one more question.
I am running this command for changing the database...
mongo --eval "use Sample_Test"
It does not work.
when I am inserting some data. It works, but, inserts in the default database, test.
mongo --eval "db.restaurants.insert({'Name':'Vishal'})"
I want to insert data in Sample_Test Database.
I only know above "mongo --eval" command, if anybody knows some more, please share it.....it would really help.
The data is getting inserted to default database as your use DB is not working here.
You need to add database name here using -d if you are using older version of MongoDB like following :
mongo -d Sample_Test --eval "db.restaurants.insert({'Name':'Vishal'})"
or you can execute any script file which contains all your mongo script like following:
mongo -d Sample_Test --eval mongoScript.js
In Mongo version 3.2, -d flag is not needed. For more information about script refer documentation
So you can simply use
mongo Sample_Test --eval "db.restaurants.insert({'Name':'Vishal'})"
Hope this will work for you.
use this code on place of your code:
mongo.exe Sample_Test --eval "db.restaurants.insert({'Name':'Vishal'})"
but if you want to run multiple commands in one session login use script file.
You can also use the URL style to connect to a MongoDb server and database like:
mongo "mongoldb://localhost:8000/myCars" --eval "db.getCollection('restaurants').insert({'Name':'Vishal'})
You can also put credentials into the URL.
See: https://docs.mongodb.com/manual/mongo/

Can't create mongodb db

According to this documentation using use mays should create a db if it isn't already created:
MongoDB use DATABASE_NAME is used to create database. The command will
create a new database, if it doesn't exist otherwise it will return
the existing database.
So why doesn't mongo use mays work?
root#server88-208-249-95:~# mongo use mays
MongoDB shell version: 2.6.11
connecting to: use
2015-09-16T22:17:20.316+0100 file [mays] doesn't exist
failed to load: mays
The 'use' command doesn't work with mongo command.
You have to open mongo shell and then use the 'use' command.
Open terminal -> enter 'mongo' to get mongo shell ->
use db_name
This will create a DB if it doesn't exists already.
A DB doesn't show up when using 'show dbs' until you create a collection in it.
Use db.createCollection("collection_name") and then use 'show dbs' and you will see your newly created DB.
You need to create at least one collection before it saves the database. Issuing a use will create it, but will not automatically save it. You can create an empty collection with db.createCollection("test"). To verify try the following commands from the mongo shell:
use mays
show dbs (mays will not show up)
db.createCollection("test")
show dbs (mays will show up)

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.