MongoDB Flashcards
The base of traditional relational database management systems:
Atomicity: All operations in a transactionsucceeds or rolled back.
Consistency: After a transaction completed, the database remains consistence.
Isolation: Transactions are executed without interfering with other transactions.
Durability: After a transaction completed, changes will be permanent.
Define NoSQL
No SQL is a generic term that refers to any non-relational data management system and does not use SQL query language.
MongoDB features:
Indexing
Aggregations
File Storage
Special collection types
Create a collection named “newCollection” and insert the field “field” and the value “value :
> db.newCollection.insertOne({“field” : “value”})
Create a database named “myNewDb”
> use myNewDb
Drop the current database
db.dropDatabase()
Add one document to the collection “TestKey” with an id of 1 and a name of “test2”
db.TestKey.insertOne({_id:1, Name: “test2”})
The command to insert multiple documents into a collection:
db.collection.insertMany([
{ // fields and values } ,
{ // fields and values }
])
display the “Employee” collection
db.Employee.find()
OR
db.Employee.find().pretty()
display all databases
show dbs
display collections of current database
show collections
show all the collections in an array:
db.getCollectionNames()
drop the collection “Dept”
db.Dept.drop()
create a collection named “Room”
db.createCollection(“Room”)
Remove all documents in the collection “foo”
db.foo.remove()
remove all documents from the mailing.list collection where the value “opt-out” equals “true”
db.mailing.list.remove({“opt-out” : true })
Delete Documents for one and for many
db. collection.deleteOne({//fields and values})
db. collection.deleteMany()
Update the documents in the collection “Course” that have the “NumOfHours” value as 48. Change the CourseName to “OOP 1”
db.Course.updateOne(
{NumOfHours : 48},
{$set : {CourseName : “OOP 1”}}
)
Update multiple documents from the collection “Course” where NumOfHours is 48 and Program.Duration is 3 years. Set the CourseName to “System Design”
db.Course.updateMany(
{ $and : [ {NumOfHours: 48},
{Program.Duration : “3 years”} ] },
{$set : {CourseName: “System Design”}} )
What are the comparison operators for : < <= > >= !=
$lt $lte $gt $gte $ne
Query employees who are between the ages of 25 and 40
db.Employee.find({age: {$gte: 25, $lte 40 }})
Find the employees details who are earning less or equal to 2000
db.Employee.find({salary: {$lte : 2000}})
Find all employees details who are not working HR department.
db.Employee.find({department: {$ne : “HR”}})
find employees with a salary of either 1200, 2000, or 2500
db.Employee.find({salary: {$in : [1200, 2000, 2500] }})
find employees that DON’T have a salary of either 1200, 2000, or 2500
db.Employee.find({salary: {$nin : [1200, 2000, 2500]}})
Display the name of employees where the department name is not HR and the age is 55
db.Employee.find({department.name: {$ne : “HR”}, age: 55}, {name: 1})