How to load initial data in MongoDB? - mongodb

Does anyone know how to populate mongodb with initial data? For example, with a traditional SQL database, you can put all your SQL statements in a textfile, then load that using a SQL command. This is extremely useful for unit testing.
Is it possible to do this with the mongo shell? For example, write down a list of shell statements into a file, and get the mongo shell to read the file and execute the statements.

./mongo server:27017/dbname --quiet my_commands.js
In my_commands.js:
db.users.insert({name:"john", email:"john#doe.com", age:12});

You can use the mongoimport tool that comes with MongoDB to import raw data.
To run scripts from a file, e.g. to recreate indexes, pass the file name as a command line argument:
mongo file.js "another file.js"

Related

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/

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"});

Creating a subset of an existing mongo db database

This query is related to building a small MongoDB test database from a large existing database.
My plan to execute this is as follows:
a) Use mongodump with an aggregate query which specifies my conditions for the records to be copied over to the test database.
Will this idea work? From what I have read on forums, using a MongoDB query as is in a mongodump command will not work.
Any guidance on this is most appreciated.
You can use the following command to get the subset of the DB.
mongodump --query "your query here"
For more information read the mongodump documentation here.

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.

Some beginner's questions about MongoDB

I'm a beginner with MongoDB and I've some questions:
When I'm connected to Mongo, and i execute show dbs I see 2 databases: admin and local. What's their role? Then if I execute an insert command like db.foo.insert({"value":"mongo"}), the test database appears. Why? How can i specify a custom name for a database?
With show dbs I get the databases (somehow like show databases in sql), how can I then list the collections inside a database (I would use show tables in sql)?
When executing a command, the MongoDB tutorial always uses the db object. Is it the main object (a sort of "connection" object) that has to used for executing commands or it's something else?
Thanks!
admin and local contain various settings local to the server, like users who are authenticated to connect. Under beginner usage, you shouldn't need to worry about them at all. By default you connect to a database named test. To connect to a new database, just use databasename from the mongo command line, or mongo databasename from your OS shell.
use [database_name] and then show collections
The db object is your root handle to the currently-selected database on the mongo commmand line. The command line is really just a Javascript command line, and there are various mongodb-specific objects and functions exposed that let you do stuff. Try help() for a full listing.