Assignments Of Day 19: MongoDB Data Import and Export
Assignment 1: Import JSON Data into MongoDB
Objective: Learn how to import a JSON file into a MongoDB collection.
Instructions:
1. Create a products.json file with the following content:
json[ { "product_name": "Laptop", "price": 1000, "category": "Electronics" }, { "product_name": "Shirt", "price": 30, "category": "Clothing" }]2. Import this JSON file into a MongoDB database named shop and a collection named inventory.
Solution:
1. Save the products.json file with the content mentioned above.
2. Open the terminal and run the following mongoimport command:
bashmongoimport --db shop --collection inventory --file products.json --type json --jsonArray3. Explanation:
o --db shop: Specifies the database shop.
o --collection inventory: Specifies the collection inventory.
o --file products.json: Points to the products.json file.
o --type json: Specifies the data format as JSON.
o --jsonArray: Tells MongoDB that the JSON file contains an array.
4. Result: The data will be imported into the inventory collection in the shop database.
Assignment 2: Import CSV Data into MongoDB
Objective: Learn how to import CSV data into MongoDB.
Instructions:
1. Create a employees.csv file with the following content:
csvname,age,departmentJohn,30,EngineeringJane,28,Marketing2. Import this CSV file into the MongoDB database company and collection staff.
Solution:
1. Save the employees.csv file with the content mentioned above.
2. Open the terminal and run the following mongoimport command:
bashmongoimport --db company --collection staff --file employees.csv --type csv --headerline3. Explanation:
o --db company: Specifies the database company.
o --collection staff: Specifies the collection staff.
o --file employees.csv: Points to the employees.csv file.
o --type csv: Specifies the data format as CSV.
o --headerline: Uses the first row as field names.
4. Result: The CSV data will be imported into the staff collection in the company database.
Assignment 3: Import Data and Skip Duplicates
Objective: Learn how to prevent importing duplicate records during the import process.
Instructions:
1. Create a students.json file with the following content:
json[ { "name": "Alice", "age": 22, "major": "Physics" }, { "name": "Bob", "age": 23, "major": "Mathematics" }]2. Import this data into a MongoDB database named university and a collection named students.
3. Run the import again, but ensure that any duplicate records are skipped.
Solution:
1. Save the students.json file.
2. Open the terminal and run the following mongoimport command:
bashmongoimport --db university --collection students --file students.json --type json --jsonArray --upsert3. Explanation:
o --upsert: Ensures that if the document already exists based on the _id, it will be updated; otherwise, a new document will be inserted. This helps to skip duplicates if they already exist.
o If you want to import data without creating duplicates based on a unique field, you can use an index on that field.
4. Result: If the data already exists in the collection, the --upsert flag will update it instead of duplicating.
Assignment 4: Import JSON Data with Nested Structure
Objective: Learn how to import JSON data with nested objects.
Instructions:
1. Create a orders.json file with the following content:
json[ { "order_id": "001", "customer": { "name": "John", "email": "john@example.com" }, "total": 150 }, { "order_id": "002", "customer": { "name": "Jane", "email": "jane@example.com" }, "total": 200 }]2. Import this data into the MongoDB database shop and collection orders.
Solution:
1. Save the orders.json file.
2. Open the terminal and run the following mongoimport command:
bashmongoimport --db shop --collection orders --file orders.json --type json --jsonArray3. Explanation:
o MongoDB supports nested documents (like customer in this case) without any special handling.
o The nested structure will be preserved during import.
4. Result: The data with nested structures will be imported successfully into the orders collection.
Assignment 5: Export Data from MongoDB to JSON
Objective: Learn how to export data from a MongoDB collection to a JSON file.
Instructions:
1. Export all data from the students collection in the university database to a JSON file named students_export.json.
Solution:
1. Open the terminal and run the following mongoexport command:
bashmongoexport --db university --collection students --out students_export.json --type json2. Explanation:
o --db university: Specifies the database university.
o --collection students: Specifies the collection students.
o --out students_export.json: Specifies the output file students_export.json.
o --type json: Exports the data in JSON format.
3. Result: The entire students collection will be exported to the students_export.json file.
Assignment 6: Export Specific Fields from MongoDB
Objective: Learn how to export only specific fields from a MongoDB collection.
Instructions:
1. Export only the name and age fields from the students collection in the university database to a CSV file named students_partial.csv.
Solution:
1. Open the terminal and run the following mongoexport command:
bashmongoexport --db university --collection students --out students_partial.csv --type csv --fields name,age2. Explanation:
o --fields name,age: Specifies that only the name and age fields should be exported.
o --type csv: Specifies that the output format should be CSV.
3. Result: The name and age fields of the students collection will be exported to students_partial.csv.
Assignment 7: Filter Data During Export
Objective: Learn how to filter data during export using a query.
Instructions:
1. Export students who are older than 22 from the students collection in the university database to a JSON file.
Solution:
1. Open the terminal and run the following mongoexport command:
bashmongoexport --db university --collection students --out filtered_students.json --type json --query '{"age": {"$gt": 22}}'2. Explanation:
o --query '{"age": {"$gt": 22}}': Filters the data to export only students who are older than 22 years.
3. Result: The data for students who are older than 22 will be exported to filtered_students.json.
Assignment 8: Import CSV Data with Custom Delimiters
Objective: Learn how to import CSV data when using a custom delimiter.
Instructions:
1. Create a products.csv file with the following content, using | as the delimiter:
csvproduct_name|price|categoryLaptop|1000|ElectronicsShirt|30|Clothing2. Import this data into MongoDB in the shop database and inventory collection.
Solution:
1. Save the products.csv file.
2. Open the terminal and run the following mongoimport command:
bashmongoimport --db shop --collection inventory --file products.csv --type csv --headerline --fields product_name,price,category --delim "|"3. Explanation:
o --delim "|": Specifies the custom delimiter (| in this case).
o --headerline: Uses the first row for field names.
4. Result: The data will be imported using the custom delimiter.
Assignment 9: Import Data with Field Mapping
Objective: Learn how to map fields while importing data into MongoDB.
Instructions:
1. Create a books.csv file:
csvtitle,author,price"The Great Gatsby",F. Scott Fitzgerald,10"1984",George Orwell,152. Import this data into MongoDB, mapping the fields to book_title, book_author, and book_price.
Solution:
1. Save the books.csv file.
2. Open the terminal and run the following mongoimport command:
bashmongoimport --db library --collection books --file books.csv --type csv --headerline --fields book_title=title,book_author=author,book_price=price3. Explanation:
o --fields: Maps the original CSV columns to new field names in MongoDB.
4. Result: The fields in the MongoDB collection will be renamed according to the mappings provided.
Assignment 10: Export Data to BSON Format
Objective: Learn how to export data to BSON format.
Instructions:
1. Export all documents from the inventory collection in the shop database to a BSON file named inventory_export.bson.
Solution:
1. Open the terminal and run the following mongoexport command:
bashmongoexport --db shop --collection inventory --out inventory_export.bson --type bson2. Explanation:
o --type bson: Specifies the BSON format for the exported file.
o BSON (Binary JSON) is the native format used by MongoDB to store data.
3. Result: The data will be exported as a BSON file.
