Here is how to create Mongo Database from JSON files.
Create JSON files with data. Example, I have created a json file named players.json with following data:
and created teams.json file with the following data:
mongod
In another terminal open the folder where both the json files are there and run the following commands:
mongoimport --db ipldb --collection players --file players.json
and
mongoimport --db ipldb --collection teams --file teams.json
This will create a database named ipldb abd add two collections to it namely players and teams with three records each as found in the json files.
db.players.find()
Create JSON files with data. Example, I have created a json file named players.json with following data:
{
"first_name": "Virat",
"last_name": "Kohli",
"type": "batsman",
"age": 28,
"team": "Delhi Daredevils"
}
{
"first_name": "Mahendra",
"last_name": "Dhoni",
"type": "all rounder",
"age": 38,
"team": "Big Bengaluru"
}
{
"first_name": "Rohit",
"last_name": "Sharma",
"type": "baller",
"age": 32,
"team": "Mohali Wale"
}
{
"name": "Delhi Daredevils",
"city": "Delhi",
"won": 2
}
{
"name": "Big Bengaluru",
"city": "Bengaluru",
"won": 3
}
{
"name": "Mohali Wale",
"city": "Mohali",
"won": 0
}
Now, lets add this data to collections in a database of MongoDB.
For that open a new terminal/command prompt and run MongoDB by using the command:mongod
In another terminal open the folder where both the json files are there and run the following commands:
mongoimport --db ipldb --collection players --file players.json
and
mongoimport --db ipldb --collection teams --file teams.json
This will create a database named ipldb abd add two collections to it namely players and teams with three records each as found in the json files.
Now to view the database collections and it's documents, run the following:
mongo ipldb- This will open the database named ipldb
db.players.find()
- This will show all documents in players collection
- This will show all documents in teams collection
Comments
Post a Comment