NoSQL + MongoDB Flashcards

1
Q

Terminal commands to start the mongo server?

A

mongod –config c:\data\db01.conf

mongo –port 27018

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Difference between Schema on Read and Schema on Write?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does BASE stand for?

A

Basically Available, Soft state, Eventually consistent.

NoSQL is essentially offering High Availability with BASE, while ACID offers guaranteed Consistency

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

For MongoDB what are the following?

a. Its storage format?

b. Its Collections?

c. Its Documents?

d. Its Relationships?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In MongoDB when dealing with One-to-Many relationships, what are the 2 possible solutions?

A

Embedding one of the entities into the other in an array.
or
Using a reference to the other document in the other collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

a. db
b. use newDB
c. show dbs
d. db.test.insertOne({x:1});
e. show collections

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

Students(
{St_Name: “Sarah”,
Courses:[ {Course: Java#1}, {Course:DBAS#1} ]
},
{St_Name: “Mark”,
Courses:[ {Course: Java#1}]},

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

db.Employee.find({“department.name”:”Finance”}{name:1})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

db.Balls.find({color:{$in:[“Blue”, “White”]}} ,{_id:1})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

a.db.Employees.find()

b. db.Employees.find({“department.name”:”Sales”})

c. db.Employees.find({“department.name”:”Engineering”,
salary:{$lt: 70000}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

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”: “”}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

a. db.employees.updateMany({}, { $rename:{“job_title”:”title”}})

b. db.employees.updateMany({}. { $unset: {“title”:””}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens if no _id is specified when inserting like below:
db.Students.insertOne({name:”John”})

A

Mongo will automatically insert an ObjectId in the _id field.

ObjectId is 24 hexadecimal,
formatted like so:
ObjectId(“507f1f77bcf86cd799439011”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to drop a collection named employee in Mongo?

How about dropping a database in Mongo?

A

db.Employee.drop() to drop the collection

db.dropDatabase() to drop the current database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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’

A

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’}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the mongo Logical operators?

Using the Course collection, solve the following:

a. Change the courseName of any course that has an credit value of 3 and a monthsToComplete value greater than or equal to 4, to ‘Programming’

A

$and - and condition, syntax: $and:[ { }, { } ]
$or - or condition, syntax: $or:[ { }, { } ]
$not

a. db.Course.updateMany({$and:[{credit:3}, {monthsToComplete:{$gte: 4}}]}, {$set:{courseName:’Programming’}})