vj_time
vj_time

Reputation: 21

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.

Upvotes: 2

Views: 3343

Answers (3)

Mirko Ebert
Mirko Ebert

Reputation: 1379

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/

Upvotes: 0

Rishav Singh
Rishav Singh

Reputation: 343

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.

Upvotes: 0

Vishwas
Vishwas

Reputation: 7067

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.

Upvotes: 3

Related Questions