NoSQL + MongoDB Flashcards
Terminal commands to start the mongo server?
mongod –config c:\data\db01.conf
mongo –port 27018
Difference between Schema on Read and Schema on Write?
on Write: Using a pre-existing data model(relational databases)
on Read: Datamodel is determined as you input the documents(XML, JSON)
NoSQL/Mongo allows you to use Schema on Read, it is not ACID compliant
What does BASE stand for?
Basically Available, Soft state, Eventually consistent.
NoSQL is essentially offering High Availability with BASE, while ACID offers guaranteed Consistency
For MongoDB what are the following?
a. Its storage format?
b. Its Collections?
c. Its Documents?
d. Its Relationships?
a. BSON(Binary JSON)
b. Collection = Table in SQL
c. Documents = rows in SQL, uses _id as the identifier
d. Uses _id as the Primary key of a collection
In MongoDB when dealing with One-to-Many relationships, what are the 2 possible solutions?
Embedding one of the entities into the other in an array.
or
Using a reference to the other document in the other collection
Terminal commands in MongoDB to do the following:
a. Display current database
b. Switch to database named newDB
c. Show all databases
d. Insert one document with x: 1 in Collection named test
e. Show all collections
a. db
b. use newDB
c. show dbs
d. db.test.insertOne({x:1});
e. show collections
Given the following Collections:
Students
({St_Name:”Sarah”,Course_1:”DBAS#1”, Course_2:”Java 1”},{St_Name:”Mark”, Course_1:”Java 1”})
Courses
({Course:”Java1”}, {Course:”DBAS#1”})
Use embedding in the Students Collection instead of course_1 and course_2
Students(
{St_Name: “Sarah”,
Courses:[ {Course: Java#1}, {Course:DBAS#1} ]
},
{St_Name: “Mark”,
Courses:[ {Course: Java#1}]},
How do you query embedded/nested documents?
Assuming the following Collection
Employee(
{name:”Mark”,
department:{ name: “Finance”, manager:”Bob” }
salary: 50000})
How would u return only the name of the employees in the Finance department
db.Employee.find({“department.name”:”Finance”}{name:1})
Write a mongo query to return all balls in the Ball collection that have a color attribute value of either “Blue” or “White”. Only return the ball _id.
db.Balls.find({color:{$in:[“Blue”, “White”]}} ,{_id:1})
Solve the following mongoDB problems:
a. Given a Collection named Employees, display all from it
b. Write a statement to retrieve all employees in the Sales department. (name is a sub property of department)
c. Write a statement to find all employees with a salary less than 70000 and working in the Engineering department.
a.db.Employees.find()
b. db.Employees.find({“department.name”:”Sales”})
c. db.Employees.find({“department.name”:”Engineering”,
salary:{$lt: 70000}})
Solve the following mongoDB problems:
a. Write a statement to update the departments collection and change ‘Human Resources’
to ‘HR’.
b. Write a statement to delete all employees in the Sales department with a salary less than 60000. (name is a sub property of department)
c. Write a statement to add a new field named ‘job_title’ to the employees collection.
a. db.departments.updateOne({name:”Human Resources”},
{ $set: {name:”HR”} })
b. db.Employees.deleteMany({“department.name”:”Sales”,
“salary”:{$lt: 60000}})
c. db.Employees.updateMany({}, {$set:{“job_title”: “”}})
Solve the following mongoDB problems:
a. Write a statement to rename the ‘job_title’ field to ‘Title’ in the employees collection.
b. Write a statement to delete the ‘Title’ field from the employees collection
a. db.employees.updateMany({}, { $rename:{“job_title”:”title”}})
b. db.employees.updateMany({}. { $unset: {“title”:””}})
What happens if no _id is specified when inserting like below:
db.Students.insertOne({name:”John”})
Mongo will automatically insert an ObjectId in the _id field.
ObjectId is 24 hexadecimal,
formatted like so:
ObjectId(“507f1f77bcf86cd799439011”)
How to drop a collection named employee in Mongo?
How about dropping a database in Mongo?
db.Employee.drop() to drop the collection
db.dropDatabase() to drop the current database
What are mongo comparison operators?
Using the Employee collection, solve the following
a. Find all employees with a salary greater than 60000
b. Change the names of any employee named ‘Bob’, ‘Mike’, or ‘Lee’ to ‘Bad name’
COMPARISON:
$eq - equal to
$gt - greater than
$gte - greater than or equal
$lt - less than
$lte - less than or equal
$in - in list
$nin - not in list
a. db.Employee.find({salary:{$gt:60000}})
b. db.Employee.updateMany({name:{$in:[‘Bob’, ‘Mike’, ‘Lee’]}}, {$set: {name:’Bad name’}})