MongoDB and Batch script operation - mongodb

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/

Related

Unable to get run js from command to query mongodb

Hi I am trying to run a simple JavaScript program to retrieve data from mongodb. collection named news.
var doc=db.news.findone();
printjson(doc);
I have mongodb running in my machine. when I try to run it from my command prompt, I am getting the below result.
MongoDB shell version:2.4.15-pre-
connecting to: test
I have no idea why it is connecting to test. Someone please help.
I assume, the collection, "news" resides on some mongo logical database, which need to be specified in script. For exmaple
db = db.getSiblingDB('db_name')
doc = db.news.findOne()
printjson(doc)
Then evaluate the script using
mongo --host ${host} --port ${port} script.js
If you don't specify a database in your connection string like for example,
//Connect to foo
${host}:${port}/foo
mongo will always try to connect to "test" database.

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

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.

How to query MongoDB without switching to the database first

When I connect to MongoDB from the command-line using the mongo command, I need to issue a use <db> command to switch to the appropriate database before I can run a query, like this:
[mongo#mongotest ~] $ mongo localhost:10001
MongoDB shell version: 2.4.6
connecting to: localhost:10001/test
> use mydb01
switched to db mydb01
Now I can run my query:
> db.records.find({"contact.name": "Jack"});
Is there a way to consolidate those commands into a single command? For example:
> mydb01.records.find({"contact.name": "Jack"});
The MongoDB shell documentation makes it seem like there should be a way, but I haven't been able to find it.
You can get a database object of another database than the one your are useing right now with db.getSiblingDB.
> db.getSiblingDB("mydb01").records.find({"contact.name": "Jack"});
You can also store that object in a variable. That way you can easily work with many databases simultaneously.
> var mydb01 = db.getSiblingDB("mydb01");
> mydb01.records.find({"contact.name": "Jack"});

Heroku: Storing local MongoDB to MongoLab

It might be a dead simple question yet I still wanted to ask. I've created a Node.js application and deployed it on Heroku. I've also set up the database connection without having any trouble as well.
However, I cannot get the load the local data in my MongoDB to MongoLab I use on heroku. I've searched google and could not find a useful solution so I ended up trying these commands;
mongodump
And:
mongorestore -h mydburl:mydbport -d mydbname -u myusername -p mypassword --db Collect.1
Now when I run the command mongorestore, I received the error;
ERROR: multiple occurrences
Import BSON files into MongoDB.
When I take a look at the DB file for MongoDB I've specified and used during the local development, I see that there are files Collect.0, Collect.1 and Collect.ns. Now I know that my db name is 'Collect' since when I use the shell I always type `use Collect`. So I specified the db as Collect.1 in command line but I still receive the same errors. Should I remove all the other collect files or there is another way around?
You can't use 'mongorestore' against the raw database files. 'mongorestore' is meant to work off of a dump file generated by 'mongodump'. First us 'mongodump' to dump your local database and then use 'mongorestore' to restore that dump file.
If you go to the Tools tab in the MongoLab UI for your database, and click 'Import / Export' you can see an example of each command with the correct params for your database.
Email us at support#mongolab.com if you continue to have trouble.
-will
This can done by two steps.
1.Dump the database
mongodump -d mylocal_db_name -o dump/
2.Restore the database
mongorestore -h xyz.mongolab.com:12345 -d remote_db_name -u username -p password dump/mylocal_db_name/

Copying mongodb

I have a web service application built using NodeJS. I am using mongodb as database.
The whole thing is in another system(A) in the network. Now I need to copy all the data to the mongodb database in my system(B).
Can anyone tell me the whole procedure in detail, like even from where I should execute the commands also. I am a beginner and have very little knowledge about mongodb.
You can use the db.cloneDatabase() command for that (docs).
Start the mongo shell from the command line on the destination server by running: mongo
Identify the name of the database to copy: use name
Copy the database from the source server: db.cloneDatabase("1.2.3.4")
Use mongodump and mongorestore utility.